Created
May 27, 2014 13:18
-
-
Save nanaya/95bde0c601f3b50bf9d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
# | |
# Idea from http://jsfiddle.net/G83mW/1/ | |
# (but less smart) | |
# | |
# Sparked off http://blogs.msdn.com/b/oldnewthing/archive/2014/05/26/10528351.aspx | |
# | |
require "benchmark" | |
def find_manual(arr) | |
lowest = arr[0] | |
arr[1..-1].each do |val| | |
lowest = val if val < lowest | |
end | |
lowest | |
end | |
def find_reduce(arr) | |
arr[1..-1].reduce(arr[0]) { |lowest, val| val < lowest ? val : lowest } | |
end | |
def find_native(arr) | |
arr.min | |
end | |
def rand_double(count) | |
count.times.map { rand } | |
end | |
def rand_int(count) | |
count.times.map { rand(count) } | |
end | |
[100_000, 200_000, 500_000, 1_000_000].each do |count| | |
arr_int = rand_int(count) | |
arr_double = rand_double(count) | |
[arr_int, arr_double].each do |arr| | |
puts ">>> Mode: #{count} / #{arr.first.class}" | |
puts "Manual: #{find_manual(arr)}" | |
puts "Reduce: #{find_reduce(arr)}" | |
puts "Native: #{find_native(arr)}" | |
Benchmark.bmbm do |x| | |
x.report("manual") { 1_000.times { find_manual(arr); } } | |
x.report("reduce") { 1_000.times { find_reduce(arr); } } | |
x.report("native") { 1_000.times { find_native(arr); } } | |
end | |
puts | |
end | |
end |
JRuby 1.7
### JRuby 1.7, Ubuntu 14.04, Java 1.7.0_55
### JRUBY_OPTS="-J-XX:ReservedCodeCacheSize=256m -J-Xmn512m -J-Xms2048m -J-Xmx2048m -Xcompile.invokedynamic=true -J-server --2.0"
>>> Mode: 100000 / Fixnum
Manual: 0
Reduce: 0
Native: 0
Rehearsal ------------------------------------------
manual 5.360000 0.130000 5.490000 ( 5.188000)
reduce 9.090000 0.120000 9.210000 ( 9.181000)
native 3.370000 0.020000 3.390000 ( 3.364000)
-------------------------------- total: 18.090000sec
user system total real
manual 4.850000 0.020000 4.870000 ( 4.877000)
reduce 8.790000 0.030000 8.820000 ( 8.828000)
native 3.260000 0.020000 3.280000 ( 3.261000)
>>> Mode: 100000 / Float
Manual: 8.824789033967662e-07
Reduce: 8.824789033967662e-07
Native: 8.824789033967662e-07
Rehearsal ------------------------------------------
manual 4.930000 0.020000 4.950000 ( 4.951000)
reduce 8.660000 0.070000 8.730000 ( 8.719000)
native 4.100000 0.030000 4.130000 ( 4.100000)
-------------------------------- total: 17.810000sec
user system total real
manual 5.040000 0.030000 5.070000 ( 5.008000)
reduce 8.750000 0.060000 8.810000 ( 8.735000)
native 4.090000 0.020000 4.110000 ( 4.091000)
>>> Mode: 200000 / Fixnum
Manual: 1
Reduce: 1
Native: 1
Rehearsal ------------------------------------------
manual 10.260000 0.050000 10.310000 ( 10.265000)
reduce 16.740000 0.130000 16.870000 ( 16.687000)
native 7.570000 0.040000 7.610000 ( 7.577000)
-------------------------------- total: 34.790000sec
user system total real
manual 9.860000 0.060000 9.920000 ( 9.934000)
reduce 16.480000 0.110000 16.590000 ( 16.580000)
native 7.570000 0.040000 7.610000 ( 7.612000)
>>> Mode: 200000 / Float
Manual: 3.311754391499555e-06
Reduce: 3.311754391499555e-06
Native: 3.311754391499555e-06
Rehearsal ------------------------------------------
manual 9.910000 0.060000 9.970000 ( 9.969000)
reduce 17.110000 0.130000 17.240000 ( 17.001000)
native 8.070000 0.060000 8.130000 ( 8.070000)
-------------------------------- total: 35.340000sec
user system total real
manual 9.990000 0.040000 10.030000 ( 9.992000)
reduce 16.910000 0.130000 17.040000 ( 16.905000)
native 8.040000 0.030000 8.070000 ( 7.979000)
>>> Mode: 500000 / Fixnum
Manual: 0
Reduce: 0
Native: 0
Rehearsal ------------------------------------------
manual 25.850000 0.150000 26.000000 ( 25.915000)
reduce 44.360000 0.340000 44.700000 ( 44.069000)
native 19.470000 0.100000 19.570000 ( 19.493000)
-------------------------------- total: 90.270000sec
user system total real
manual 25.370000 0.120000 25.490000 ( 25.500000)
reduce 42.150000 0.280000 42.430000 ( 42.136000)
native 19.040000 0.080000 19.120000 ( 19.120000)
>>> Mode: 500000 / Float
Manual: 1.1895202227663049e-06
Reduce: 1.1895202227663049e-06
Native: 1.1895202227663049e-06
Rehearsal ------------------------------------------
manual 26.290000 0.110000 26.400000 ( 26.125000)
reduce 42.910000 0.320000 43.230000 ( 42.366000)
native 20.910000 0.150000 21.060000 ( 20.814000)
-------------------------------- total: 90.690000sec
user system total real
manual 26.350000 0.160000 26.510000 ( 26.369000)
reduce 42.430000 0.290000 42.720000 ( 42.550000)
native 21.070000 0.140000 21.210000 ( 21.097000)
>>> Mode: 1000000 / Fixnum
Manual: 0
Reduce: 0
Native: 0
Rehearsal ------------------------------------------
manual 57.130000 0.320000 57.450000 ( 57.389000)
reduce 84.380000 0.670000 85.050000 ( 83.942000)
native 38.800000 0.290000 39.090000 ( 39.007000)
------------------------------- total: 181.590000sec
user system total real
manual 56.990000 0.340000 57.330000 ( 57.322000)
reduce 84.700000 0.820000 85.520000 ( 84.318000)
native 39.360000 0.230000 39.590000 ( 39.433000)
>>> Mode: 1000000 / Float
Manual: 5.931643176637635e-09
Reduce: 5.931643176637635e-09
Native: 5.931643176637635e-09
Rehearsal ------------------------------------------
manual 52.380000 0.230000 52.610000 ( 52.489000)
reduce 85.550000 0.660000 86.210000 ( 85.188000)
native 41.270000 0.240000 41.510000 ( 41.339000)
------------------------------- total: 180.330000sec
user system total real
manual 51.760000 0.280000 52.040000 ( 52.006000)
reduce 85.400000 0.640000 86.040000 ( 84.655000)
native 40.820000 0.250000 41.070000 ( 41.063000)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I thought ruby built-in function is faster. Except it's not - it's on par and slower to
each
loop onFixnum
s andFloat
s respectively. And that sweet.reduce
function? Slow, it seems.