examples/heap_push.rb in compsci-0.2.0.1 vs examples/heap_push.rb in compsci-0.3.0.1

- old
+ new

@@ -1,41 +1,55 @@ require 'compsci/heap' require 'compsci/timer' include CompSci +runtime = (ARGV.shift || "3").to_i + puts <<EOF # # 3 seconds worth of pushes # EOF +RANDMAX = 1_000 +NUMBERWANG = 1_000 +NUMS = (0..(RANDMAX - 1)).to_a.shuffle + +def number(num) + if num % NUMBERWANG == 0 + NUMS.shift + NUMS.push rand RANDMAX + end + NUMS[num % RANDMAX] +end + count = 0 start = Timer.now start_100k = Timer.now h = Heap.new loop { count += 1 if count % 10000 == 0 - _answer, push_elapsed = Timer.elapsed { h.push rand 99999 } + _answer, push_elapsed = Timer.elapsed { h.push number(count) } puts "%ith push: %0.8f s" % [count, push_elapsed] if count % 100000 == 0 - h.push rand 99999 + h.push number(count) push_100k_elapsed = Timer.since start_100k puts "-------------" puts " 100k push: %0.8f s (%ik push / s)" % [push_100k_elapsed, 100.to_f / push_100k_elapsed] puts start_100k = Timer.now end else - h.push rand 99999 + h.push number(count) end - break if Timer.since(start) > 3 + break if Timer.since(start) > runtime } puts "pushed %i items in %0.1f s" % [count, Timer.since(start)] puts