HEX
Server: Apache
System: Linux s198.coreserver.jp 5.15.0-151-generic #161-Ubuntu SMP Tue Jul 22 14:25:40 UTC 2025 x86_64
User: nagasaki (10062)
PHP: 7.1.33
Disabled: NONE
Upload Files
File: //usr/local/rvm/src/ruby-3.0.2/sample/benchmark.rb
require 'benchmark'

include Benchmark

n = ARGV[0].to_i.nonzero? || 50000
puts %Q([#{n} times iterations of `a = "1"'])
benchmark(CAPTION, 7, FORMAT) do |x|
  x.report("for:")   {for _ in 1..n; _ = "1"; end} # Benchmark.measure
  x.report("times:") {n.times do   ; _ = "1"; end}
  x.report("upto:")  {1.upto(n) do ; _ = "1"; end}
end

benchmark do
  [
    measure{for _ in 1..n; _ = "1"; end},  # Benchmark.measure
    measure{n.times do   ; _ = "1"; end},
    measure{1.upto(n) do ; _ = "1"; end}
  ]
end