Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'set'
- require 'benchmark'
- ELEMENTS = (ARGV[0] || 10_000_000).to_i
- Benchmark.bmbm( 24 ){|test|
- test.report('Init Hash'){
- @hash = {}
- for i in (1 .. ELEMENTS).to_a.shuffle
- @hash[i] = i
- end
- }
- test.report('Init Set'){
- @set = Set.new( (1 .. ELEMENTS).to_a.shuffle )
- }
- test.report('Init Set (loop)'){
- s = Set.new
- for i in (1 .. ELEMENTS).to_a.shuffle
- s << i
- end
- }
- test.report('Init Array'){
- @array = (1 .. ELEMENTS).to_a.shuffle
- }
- test.report('Init Array (loop)'){
- a = []
- for i in (1 .. ELEMENTS).to_a.shuffle
- a << i
- end
- }
- test.report('Enum. Hash'){
- for (k, v) in @hash
- k * v
- end
- }
- test.report('Enum. Hash (keys)'){
- for k in @hash.keys
- @hash[k]
- end
- }
- test.report('Enum. Hash (rand keys)'){
- for k in @hash.keys.shuffle
- @hash[k]
- end
- }
- test.report('Enum. Set'){
- for v in @set
- v * v
- end
- }
- test.report('Enum. Set (rand)'){
- for v in @set.to_a.shuffle
- v * v
- end
- }
- test.report('Enum. Array'){
- for v in @array
- v * v
- end
- }
- test.report('Enum. Array (rand)'){
- for v in @array.shuffle
- v * v
- end
- }
- test.report('Enum. Set -> Array'){
- for v in @set.to_a
- v * v
- end
- }
- test.report('Find Hash'){
- for i in (1 .. ELEMENTS).to_a.shuffle
- @hash[i]
- end
- }
- test.report('Find Set'){
- for i in (1 .. ELEMENTS).to_a.shuffle
- @set.include? i
- end
- }
- test.report('Find Array'){
- for i in (1 .. ELEMENTS).to_a.shuffle
- @array[i]
- end
- }
- }
Add Comment
Please, Sign In to add comment