bench/bench.rb in lru_redux-0.8.4 vs bench/bench.rb in lru_redux-1.1.0

- old
+ new

@@ -1,45 +1,43 @@ -require 'rubygems' -require 'lru' +require 'bundler' require 'benchmark' +require 'lru' require 'lru_cache' require 'threadsafe-lru' -$LOAD_PATH.unshift File.expand_path '../lib' -require File.expand_path('../../lib/lru_redux', __FILE__) +Bundler.require + +# Lru lru = Cache::LRU.new(max_elements: 1_000) + +# LruCache lru_cache = LRUCache.new(1_000) -lru_redux = LruRedux::Cache.new(1_000) -lru_redux_thread_safe = LruRedux::ThreadSafeCache.new(1_000) +# ThreadSafeLru thread_safe_lru = ThreadSafeLru::LruCache.new(1_000) -bm = Benchmark.bmbm do |bm| +# LruRedux +redux = LruRedux::Cache.new(1_000) +redux_thread_safe = LruRedux::ThreadSafeCache.new(1_000) - bm.report "thread safe lru" do - 1_000_000.times do - thread_safe_lru.get(rand(2_000)){ :value } - end +puts "** LRU Benchmarks **" +Benchmark.bmbm do |bm| + bm.report 'ThreadSafeLru' do + 1_000_000.times { thread_safe_lru.get(rand(2_000)) { :value } } end - [ - [lru, "lru gem"], - [lru_cache, "lru_cache gem"], - ].each do |cache, name| - bm.report name do - 1_000_000.times do - cache[rand(2_000)] ||= :value - end - end + bm.report 'LRU' do + 1_000_000.times { lru[rand(2_000)] ||= :value } end - [ - [lru_redux, "lru_redux gem"], - [lru_redux_thread_safe, "lru_redux thread safe"] - ].each do |cache, name| - bm.report name do - 1_000_000.times do - cache.getset(rand(2_000)) { :value } - end - end + bm.report 'LRUCache' do + 1_000_000.times { lru_cache[rand(2_000)] ||= :value } + end + + bm.report 'LruRedux::Cache' do + 1_000_000.times { redux.getset(rand(2_000)) { :value } } + end + + bm.report 'LruRedux::ThreadSafeCache' do + 1_000_000.times { redux_thread_safe.getset(rand(2_000)) { :value } } end end