Skip to content

Instantly share code, notes, and snippets.

@k0kubun

k0kubun/loop.rb Secret

Last active May 3, 2020 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0kubun/10e6d3387c9ab1b134622b2c9d76ef51 to your computer and use it in GitHub Desktop.
Save k0kubun/10e6d3387c9ab1b134622b2c9d76ef51 to your computer and use it in GitHub Desktop.
require 'erb'
require 'shellwords'
methods = Integer(ARGV[0] || 1)
sleep_seconds = methods * 0.15
loop_count = (250000000.0 / methods).to_i
src = ERB.new(<<-'erb', nil, '%').result(binding)
% methods.times do |i|
def call<%= i %>() nil end
call<%= i %>; call<%= i %>; call<%= i %>; call<%= i %>; call<%= i %>;
% end
if ENV['MJIT_ENABLE'] == 'true'
$stderr.puts "sleeping <%= sleep_seconds %>"
sleep <%= sleep_seconds %>
end
$stderr.puts "running"
i = 0
before = Process.clock_gettime(Process::CLOCK_MONOTONIC)
while i < <%= loop_count %>
<%= methods.times.map { |i| "call#{i}; " }.join('') %>
i += 1
end
after = Process.clock_gettime(Process::CLOCK_MONOTONIC)
puts((after - before).to_s)
$stderr.puts "finish\n"
erb
puts "\n```\n#{src}```\n\n"
results = []
3.times do
if ENV['MJIT_ENABLE'] == 'true'
cmd = [RbConfig.ruby, '--disable-gems', '--jit-verbose=1', '--jit-max-cache=10000', '-e']
else
cmd = [RbConfig.ruby, '--disable-gems', '-e']
end
puts "+ #{cmd.shelljoin} ..."
result = IO.popen([*cmd, src], &:read)
results << result
puts "\n\nResult: #{result}\n\n"
end
puts "Avg: #{results.map(&:strip).map(&:to_f).inject(&:+) / results.size.to_f}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment