Skip to content

Instantly share code, notes, and snippets.

@yuki24
Last active August 29, 2015 14:11
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 yuki24/e8d89597efa0fbdfe775 to your computer and use it in GitHub Desktop.
Save yuki24/e8d89597efa0fbdfe775 to your computer and use it in GitHub Desktop.
Array#max vs if
require 'benchmark/ips'
Benchmark.ips do |x|
NEGATIVE = "-1"
ZERO = "0"
ONE = "1"
TWO = "2"
x.report("Array#max ( -1)") { [NEGATIVE.to_i, 1].max - 1 }
x.report("Array#max ( 0)") { [ ZERO.to_i, 1].max - 1 }
x.report("Array#max ( 1)") { [ ONE.to_i, 1].max - 1 }
x.report("Array#max ( 2)") { [ TWO.to_i, 1].max - 1 }
x.report("Array#max (nil)") { [ NIL.to_i, 1].max - 1 }
x.report("with if ( -1)") do
num = NEGATIVE.to_i
num < 0 ? 0 : (num - 1)
end
x.report("with if ( 0)") do
num = ZERO.to_i
num < 0 ? 0 : (num - 1)
end
x.report("with if ( 1)") do
num = ONE.to_i
num < 0 ? 0 : (num - 1)
end
x.report("with if ( 2)") do
num = TWO.to_i
num < 0 ? 0 : (num - 1)
end
x.report("with if (nil)") do
num = NIL.to_i
num < 0 ? 0 : (num - 1)
end
end
@yuki24
Copy link
Author

yuki24 commented Dec 17, 2014

Ruby 2.1.5:

$ ruby array_max_vs_if.rb 
Calculating -------------------------------------
     Array#max ( -1)    83.827k i/100ms
     Array#max (  0)    82.651k i/100ms
     Array#max (  1)    81.152k i/100ms
     Array#max (  2)    88.853k i/100ms
     Array#max (nil)    99.754k i/100ms
       with if ( -1)   126.850k i/100ms
       with if (  0)   131.651k i/100ms
       with if (  1)    63.497k i/100ms
       with if (  2)    64.265k i/100ms
       with if (nil)   130.479k i/100ms
-------------------------------------------------
     Array#max ( -1)      2.045M (±13.4%) i/s -      9.975M
     Array#max (  0)      2.138M (± 5.1%) i/s -     10.662M
     Array#max (  1)      2.153M (± 3.4%) i/s -     10.793M
     Array#max (  2)      2.127M (± 5.3%) i/s -     10.662M
     Array#max (nil)      2.590M (± 3.6%) i/s -     12.968M
       with if ( -1)      5.110M (± 4.3%) i/s -     25.497M
       with if (  0)      5.034M (± 5.4%) i/s -     25.277M
       with if (  1)      4.927M (±12.1%) i/s -     24.129M
       with if (  2)      5.053M (± 5.4%) i/s -     25.192M
       with if (nil)      7.871M (± 4.3%) i/s -     39.274M

Rubinius 2.4.1:

$ ruby array_max_vs_if.rb 
Calculating -------------------------------------
     Array#max ( -1)    25.971k i/100ms
     Array#max (  0)    46.565k i/100ms
     Array#max (  1)    46.207k i/100ms
     Array#max (  2)    45.501k i/100ms
     Array#max (nil)    48.623k i/100ms
       with if ( -1)    79.919k i/100ms
       with if (  0)    80.972k i/100ms
       with if (  1)    81.833k i/100ms
       with if (  2)    80.720k i/100ms
       with if (nil)    90.085k i/100ms
-------------------------------------------------
     Array#max ( -1)    624.191k (± 4.4%) i/s -      3.117M
     Array#max (  0)    618.419k (± 2.9%) i/s -      3.120M
     Array#max (  1)    602.022k (± 3.7%) i/s -      3.050M
     Array#max (  2)    612.837k (± 2.8%) i/s -      3.094M
     Array#max (nil)    674.274k (± 3.3%) i/s -      3.404M
       with if ( -1)      1.190M (± 3.3%) i/s -      5.994M
       with if (  0)      1.183M (± 3.8%) i/s -      5.911M
       with if (  1)      1.160M (± 3.8%) i/s -      5.810M
       with if (  2)      1.205M (± 3.2%) i/s -      6.054M
       with if (nil)      1.351M (± 4.6%) i/s -      6.756M

JRuby head:

$ ruby array_max_vs_if.rb 
Calculating -------------------------------------
     Array#max ( -1)     2.920k i/100ms
     Array#max (  0)    63.161k i/100ms
     Array#max (  1)    95.615k i/100ms
     Array#max (  2)   104.680k i/100ms
     Array#max (nil)   118.946k i/100ms
       with if ( -1)   144.596k i/100ms
       with if (  0)   129.202k i/100ms
       with if (  1)   130.889k i/100ms
       with if (  2)   130.270k i/100ms
       with if (nil)   161.872k i/100ms
-------------------------------------------------
     Array#max ( -1)      2.835M (±12.4%) i/s -     13.441M
     Array#max (  0)      3.462M (± 8.8%) i/s -     17.180M
     Array#max (  1)      3.518M (± 6.7%) i/s -     17.498M
     Array#max (  2)      3.468M (± 6.0%) i/s -     17.272M
     Array#max (nil)      4.080M (± 6.1%) i/s -     20.340M
       with if ( -1)      6.831M (± 7.5%) i/s -     33.980M
       with if (  0)      6.384M (± 7.8%) i/s -     31.784M
       with if (  1)      6.376M (± 7.7%) i/s -     31.675M
       with if (  2)      6.354M (± 7.4%) i/s -     31.656M
       with if (nil)      9.083M (± 8.5%) i/s -     45.000M

JRuby 1.7.16.2:

$ ruby array_max_vs_if.rb 
Calculating -------------------------------------
     Array#max ( -1)    85.206k i/100ms
     Array#max (  0)   149.927k i/100ms
     Array#max (  1)   161.005k i/100ms
     Array#max (  2)   162.529k i/100ms
     Array#max (nil)   176.565k i/100ms
       with if ( -1)   221.807k i/100ms
       with if (  0)   206.752k i/100ms
       with if (  1)   217.203k i/100ms
       with if (  2)   219.274k i/100ms
       with if (nil)   248.686k i/100ms
-------------------------------------------------
     Array#max ( -1)      4.013M (± 8.0%) i/s -     19.938M
     Array#max (  0)      3.873M (± 7.6%) i/s -     19.191M
     Array#max (  1)      4.002M (± 5.0%) i/s -     19.965M
     Array#max (  2)      4.014M (± 4.6%) i/s -     20.154M
     Array#max (nil)      4.893M (± 6.1%) i/s -     24.366M
       with if ( -1)      8.842M (± 7.1%) i/s -     43.918M
       with if (  0)      8.561M (± 6.8%) i/s -     42.591M
       with if (  1)      8.815M (± 6.3%) i/s -     43.875M
       with if (  2)      8.795M (± 6.2%) i/s -     43.855M
       with if (nil)     14.373M (± 8.5%) i/s -     71.124M

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