Skip to content

Instantly share code, notes, and snippets.

@bf4

bf4/bench.rb

Last active Aug 29, 2015
Embed
What would you like to do?
Benchmarking to_s vs. &&=. Result: &&= is faster
require 'benchmark/ips'
ENV = {}
Benchmark.ips do |x|
x.report("to_s, then to_s", "content_type = ENV['CONTENT_TYPE'].to_s.split(/\s*[;,]\s*/, 2).first.to_s.downcase")
x.report("to_s, then &&=", "content_type = ENV['CONTENT_TYPE'].to_s.split(/\s*[;,]\s*/, 2).first; content_type &&= content_type.downcase")
x.report("&&=, then &&=", "content_type = ENV['CONTENT_TYPE']; content_type &&= content_type.split(/\s*[;,]\s*/, 2).first; content_type &&= content_type.downcase")
end
2014 MacbookPro Mavericks, rvm
ruby-1.9.3-p545
Calculating -------------------------------------
to_s, then to_s 70050 i/100ms
to_s, then &&= 77667 i/100ms
&&=, then &&= 143708 i/100ms
-------------------------------------------------
to_s, then to_s 1269626.8 (±6.0%) i/s - 6374550 in 5.041295s
to_s, then &&= 1740026.1 (±6.0%) i/s - 8698704 in 5.020302s
&&=, then &&= 8527095.9 (±6.8%) i/s - 42393860 in 5.000842s
ruby-2.0.0-p451
Calculating -------------------------------------
to_s, then to_s 81304 i/100ms
to_s, then &&= 91575 i/100ms
&&=, then &&= 154433 i/100ms
-------------------------------------------------
to_s, then to_s 1455078.0 (±4.7%) i/s - 7317360 in 5.040880s
to_s, then &&= 1888529.5 (±6.0%) i/s - 9432225 in 5.015203s
&&=, then &&= 9195450.3 (±7.9%) i/s - 45557735 in 5.000975s
jruby-1.7.11
Calculating -------------------------------------
to_s, then to_s 147789 i/100ms
to_s, then &&= 188934 i/100ms
&&=, then &&= 300286 i/100ms
-------------------------------------------------
to_s, then to_s 3927350.8 (±5.2%) i/s - 19655937 in 5.022000s
to_s, then &&= 4511127.9 (±4.1%) i/s - 22672080 in 5.035000s
&&=, then &&= 14960140.8 (±6.3%) i/s - 74470928 in 5.003000s
rbx-2.2.6
Calculating -------------------------------------
to_s, then to_s 191145 i/100ms
to_s, then &&= 278214 i/100ms
&&=, then &&= 410259 i/100ms
-------------------------------------------------
to_s, then to_s 4369199.9 (±5.7%) i/s - 21790530 in 5.009839s
to_s, then &&= 6058620.2 (±4.4%) i/s - 30325326 in 5.017248s
&&=, then &&= 15879119.6 (±6.1%) i/s - 79179987 in 5.011392s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.