Skip to content

Instantly share code, notes, and snippets.

@casperisfine
Last active December 27, 2023 18:45
Show Gist options
  • Save casperisfine/72af4812a0cc8c925432a5d2722bc12a to your computer and use it in GitHub Desktop.
Save casperisfine/72af4812a0cc8c925432a5d2722bc12a to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips'
end
require 'benchmark/ips'
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 20
i = 1
Benchmark.ips do |x|
x.report("gettime") do
Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline
end
x.report("modulo deadcode") do
next unless (i % 100) == 0
Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline # dead code
end
x.report("modulo match") do
next unless (i % 100) == 1
Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline # dead code
end
x.compare!(order: :baseline)
end
$ ruby /tmp/clock.rb
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
Warming up --------------------------------------
gettime 1.380M i/100ms
modulo deadcode 3.024M i/100ms
modulo match 1.217M i/100ms
Calculating -------------------------------------
gettime 13.799M (± 2.0%) i/s - 69.021M in 5.003809s
modulo deadcode 30.462M (± 1.2%) i/s - 154.231M in 5.063728s
modulo match 12.186M (± 1.2%) i/s - 62.066M in 5.093831s
Comparison:
gettime: 13799327.6 i/s
modulo deadcode: 30461955.9 i/s - 2.21x faster
modulo match: 12186249.3 i/s - 1.13x slower
# ruby /app/clock.rb
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [aarch64-linux]
Warming up --------------------------------------
gettime 891.290k i/100ms
modulo deadcode 1.605M i/100ms
modulo match 768.553k i/100ms
Calculating -------------------------------------
gettime 8.544M (± 4.7%) i/s - 42.782M in 5.020463s
modulo deadcode 16.570M (± 2.9%) i/s - 83.447M in 5.040911s
modulo match 7.798M (± 1.9%) i/s - 39.196M in 5.028306s
Comparison:
gettime: 8544139.4 i/s
modulo deadcode: 16569776.6 i/s - 1.94x faster
modulo match: 7798132.3 i/s - 1.10x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment