Skip to content

Instantly share code, notes, and snippets.

@shinh
Created June 4, 2018 01:25
Show Gist options
  • Save shinh/d0008f45df2978f0bfb76a22dc92d336 to your computer and use it in GitHub Desktop.
Save shinh/d0008f45df2978f0bfb76a22dc92d336 to your computer and use it in GitHub Desktop.
loopbench
#!/usr/bin/ruby
# 65 of https://speakerdeck.com/k0kubun/the-method-jit-compiler
[1, 5, 10, 15, 20, 30, 50, 100].each do |n|
rb = "/tmp/loop_#{n}"
File.open(rb, "w") do |of|
n.times{|i|of.puts "def call#{i}() nil end"}
n.times{|i|of.puts ["call#{i}"] * 5 * '; '}
of.puts %Q(
puts "sleeping"
sleep #{6.0 * n / 40}
puts "running"
i = 0
before = Process.clock_gettime(Process::CLOCK_MONOTONIC)
while i < 4000000
)
100.times{|i|of.puts "call#{i % n}"}
of.puts %Q(
i += 1
end
after = Process.clock_gettime(Process::CLOCK_MONOTONIC)
p (after - before)
)
end
secs = `./ruby --jit #{rb}`.split[-1].to_f
puts "#{n} #{secs}"
end
* nojit
1 4.075788974063471
5 4.4973826529458165
10 4.104485634015873
15 4.152949818992056
20 4.171422621002421
30 4.07525443402119
50 4.214513655053452
100 5.068851265939884
* jit w/ trunk
1 4.999508218024857
5 4.979883964988403
10 4.921854987042025
15 4.962974144029431
20 5.161171301966533
30 5.01176204206422
50 5.277801853953861
100 10.929939543944784
* jit w/ objfcn
1 4.927098701009527
5 4.97255219402723
10 4.979578703059815
15 4.989355263067409
20 4.967847236082889
30 5.549101362004876
50 5.405622696038336
100 5.840275172027759
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment