Project

General

Profile

Feature #14984 ยป bench_when_splat.rb

O/P:Benchmark + InstructionSequence - chopraanmol1 (Anmol Chopra), 08/11/2018 06:41 PM

 
require 'benchmark'

C0_9 = (0..9).to_a
C10_18 = (10..18).to_a
C20_28 = (20..28).to_a
C30_37 = (30..37).to_a
C40_47 = (40..47).to_a
C50_56 = (50..56).to_a
C60_66 = (60..66).to_a
C70_74 = (70..74).to_a
C75_79 = (75..79).to_a
C80_83 = (80..83).to_a
C84_87 = (84..87).to_a
C90_93 = (90..93).to_a
C94_97 = (94..97).to_a

def case_splat(i)
case i
when *C0_9
0..9
when *C10_18, 19
10..19
when 29, *C20_28
20..29
when *C30_37, 38, 39
30..39
when 48, 49, *C40_47
40..49
when *C50_56, 57, 58, 59
50..59
when 67, 68, 69, *C60_66
60..69
when *C70_74, *C75_79
70..79
when *C80_83, *C84_87, 88, 89
80..89
when 98, 99, *C90_93, *C94_97
90..99
end
end

data = (0..110).to_a * 10_000
length = data.length

bm_splat= Benchmark.measure do
i = 0
while i < length
case_splat data[i]
i+=1
end
end

puts RUBY_VERSION
puts bm_splat

puts "-="*40

puts RubyVM::InstructionSequence.disasm method(:case_splat)


    (1-1/1)