Skip to content

Instantly share code, notes, and snippets.

@jodosha
Created December 13, 2018 14:23
Show Gist options
  • Save jodosha/8fcb92192cf4206a5df4033f05d22f0f to your computer and use it in GitHub Desktop.
Save jodosha/8fcb92192cf4206a5df4033f05d22f0f to your computer and use it in GitHub Desktop.
Ruby parallel assignment benchmark
#!/usr/bin/env ruby
# frozen_string_literal: true
require "benchmark/ips"
def validate
[false, "not valid"]
end
Benchmark.ips do |x|
x.report("ok,") do
ok, = validate
end
x.report("ok, _") do
ok, _ = validate
end
x.report("ok, _error") do
ok, _error = validate
end
x.compare!
end
__END__
Result:
Warming up --------------------------------------
ok, 241.064k i/100ms
ok, _ 240.399k i/100ms
ok, _error 238.295k i/100ms
Calculating -------------------------------------
ok, 6.744M (± 2.8%) i/s - 33.749M in 5.008948s
ok, _ 6.573M (± 3.3%) i/s - 32.935M in 5.017105s
ok, _error 6.641M (± 2.9%) i/s - 33.361M in 5.028197s
Comparison:
ok,: 6743781.2 i/s
ok, _error: 6640767.1 i/s - same-ish: difference falls within error
ok, _: 6572639.0 i/s - same-ish: difference falls within error
Ruby:
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin16]
Hardware:
Hardware Overview:
Model Name: MacBook Pro
Model Identifier: MacBookPro12,1
Processor Name: Intel Core i7
Processor Speed: 3,1 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache (per Core): 256 KB
L3 Cache: 4 MB
Memory: 16 GB
Boot ROM Version: 180.0.0.0.0
SMC Version (system): 2.28f7
Software:
System Software Overview:
System Version: macOS 10.12.6 (16G1618)
Kernel Version: Darwin 16.7.0
Time since boot: 10 days 6:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment