-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_push.rb
66 lines (52 loc) · 1.4 KB
/
heap_push.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'compsci/heap'
require 'compsci/timer'
include CompSci
runtime = (ARGV.shift || "3").to_i
puts <<EOF
#
# #{runtime} seconds worth of Heap pushes
#
EOF
# pregenerate a sequence of random numbers
# every NUMBERWANGth request, shift the sequence and push a new random
# this should mitigate random number generation from interfering with timing
# while also mitigating any chance of cyclic behavior
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 number(count) }
puts "%ith push: %0.8f s" % [count, push_elapsed]
if count % 100000 == 0
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 number(count)
end
break if Timer.since(start) > runtime
}
puts "pushed %i items in %0.1f s" % [count, Timer.since(start)]
puts
print "still a heap with #{h.size} items? "
answer, elapsed = Timer.elapsed { h.heap? }
puts "%s - %0.3f sec" % [answer ? 'YES' : 'NO', elapsed]
puts