Skip to content

Instantly share code, notes, and snippets.

@leonid-shevtsov
Created December 29, 2022 05:06
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 leonid-shevtsov/90d43404716aba1267262f459fb62458 to your computer and use it in GitHub Desktop.
Save leonid-shevtsov/90d43404716aba1267262f459fb62458 to your computer and use it in GitHub Desktop.
require 'concurrent/atomic/read_write_lock'
require 'benchmark/ips'
require 'active_support/core_ext/string'
rw = Concurrent::ReadWriteLock.new
Benchmark.ips do |x|
x.report("with_read_lock") do
rw.with_read_lock do
Math.sqrt(4)
end
end
x.report("with_constantize") do
'Math'.constantize.sqrt(4)
end
x.report("w/o_read_lock") do
Math.sqrt(4)
end
end
@leonid-shevtsov
Copy link
Author

      with_read_lock      1.220M (± 0.8%) i/s -      6.226M in   5.102049s
    with_constantize      6.494M (± 1.5%) i/s -     32.889M in   5.065326s
       w/o_read_lock     20.521M (± 0.6%) i/s -    104.535M in   5.094169s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment