Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move from using rbtree gem to avl_tree gem

  • Loading branch information...
commit cb43adf8af8199da03233d6a1cfdff92336f4806 1 parent 6c38466
Eric Lindvall eric authored
2  Gemfile
View
@@ -2,6 +2,8 @@ source :rubygems
gemspec
+gem 'rbtree'
+
group :test do
gem 'turn', '0.8.2', :require => false
gem 'rake', '0.8.7'
106 benchmark/samplers.rb
View
@@ -2,57 +2,91 @@
require 'benchmark'
require 'metriks'
-
-
-def fib(n)
- n < 2 ? n : fib(n-1) + fib(n-2)
-end
-
-uniform_timer = Metriks::Timer.new(Metriks::Histogram.new_uniform)
-exponential_timer = Metriks::Timer.new(Metriks::Histogram.new_exponentially_decaying)
+require 'rbtree'
+require 'avl_tree'
+require 'red_black_tree'
fib_times = ARGV[0] ? ARGV[0].to_i : 10
iter = ARGV[1] ? ARGV[1].to_i : 100000
-puts "fib(#{fib_times}): #{iter} iterations"
-puts "-" * 50
-plain = Benchmark.realtime do
- for i in 1..iter
- fib(fib_times)
+class TimerBenchmarker
+ attr_reader :iter, :fib_times
+
+ def initialize(fib_times, iter)
+ @fib_times = fib_times
+ @iter = iter
+ @mapping = { :plain => nil }
end
-end
-puts "%15s: %f secs %f secs/call" % [ 'plain', plain, plain / iter ]
+ def measure(key, value)
+ @mapping[key] = value
+ end
-uniform = Benchmark.realtime do
- for i in 1..iter
- uniform_timer.time do
- fib(fib_times)
+ def run
+ @results = {}
+ @mapping.each do |key, timer|
+ @results[key] = Benchmark.realtime do
+ if timer
+ for i in 1..iter
+ timer.time do
+ fib(fib_times)
+ end
+ end
+ else
+ for i in 1..iter
+ fib(fib_times)
+ end
+ end
+ end
end
+ report
end
-end
-puts "%15s: %f secs %f secs/call -- %.1f%% slower than plain (%f secs/call)" % [
- 'uniform', uniform, uniform / iter,
- (uniform - plain) / plain * 100 ,
- (uniform - plain) / iter,
-]
+ def report
+ results = @results.sort_by { |k,v| v }
+ results.each_with_index do |(name, time), idx|
+ puts "%23s: %f secs %f secs/call" % [
+ name, time, time / iter
+ ]
-exponential = Benchmark.realtime do
- for i in 1..iter
- exponential_timer.time do
- fib(fib_times)
+ if idx > 0
+ prev_name, prev_time = results[idx - 1]
+ puts "#{' ' * 25} - %.1f%% slower than %s (%f secs/call)" % [
+ (time - prev_time) / prev_time * 100, prev_name,
+ (time - prev_time) / iter
+ ]
+ end
+
+ if idx > 1
+ plain_name, plain_time = results[0]
+ puts "#{' ' * 25} - %.1f%% slower than %s (%f secs/call)" % [
+ (time - plain_time) / plain_time * 100, plain_name,
+ (time - plain_time) / iter
+ ]
+ end
end
end
+
+ def fib(n)
+ n < 2 ? n : fib(n-1) + fib(n-2)
+ end
end
-puts "%15s: %f secs %f secs/call -- %.1f%% slower than plain (%f secs/call) -- %.1f%% slower than uniform (%f secs/call)" % [
- 'exponential', exponential, exponential / iter,
- (exponential - plain) / plain * 100 ,
- (exponential - plain) / iter,
- (exponential - uniform) / uniform * 100 ,
- (exponential - uniform) / iter
-]
+reporter = TimerBenchmarker.new(fib_times, iter)
+
+reporter.measure :uniform, Metriks::Timer.new(Metriks::Histogram.new_uniform)
+
+reporter.measure :exponential, Metriks::Timer.new(Metriks::ExponentiallyDecayingSample.new(
+ Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA, RBTree.new))
+reporter.measure :exponential_avl, Metriks::Timer.new(Metriks::ExponentiallyDecayingSample.new(
+ Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA, AVLTree.new))
+
+reporter.measure :exponential_red_black, Metriks::Timer.new(Metriks::ExponentiallyDecayingSample.new(
+ Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA, RedBlackTree.new))
+
+puts "fib(#{fib_times}): #{iter} iterations"
+puts "-" * 50
+reporter.run
6 lib/metriks/exponentially_decaying_sample.rb
View
@@ -1,13 +1,13 @@
require 'atomic'
-require 'rbtree'
+require 'red_black_tree'
require 'metriks/snapshot'
module Metriks
class ExponentiallyDecayingSample
RESCALE_THRESHOLD = 60 * 60 # 1 hour
- def initialize(reservoir_size, alpha)
- @values = RBTree.new
+ def initialize(reservoir_size, alpha, values = nil)
+ @values = values || RedBlackTree.new
@count = Atomic.new(0)
@next_scale_time = Atomic.new(0)
@alpha = alpha
4 metriks.gemspec
View
@@ -41,11 +41,11 @@ Gem::Specification.new do |s|
## that are needed for an end user to actually USE your code.
s.add_dependency('atomic', ["~> 1.0"])
s.add_dependency('hitimes', [ "~> 1.1"])
- s.add_dependency('rbtree', [ "~> 0.3" ])
+ s.add_dependency('avl_tree', [ "~> 1.1.1" ])
## List your development dependencies here. Development dependencies are
## those that are only needed during development
- s.add_development_dependency('tomdoc', ["~> 0.2"])
+ # s.add_development_dependency('tomdoc', ["~> 0.2"])
s.add_development_dependency('mocha', ['~> 0.10'])
## Leave this section as-is. It will be automatically generated from the
Please sign in to comment.
Something went wrong with that request. Please try again.