Skip to content

Instantly share code, notes, and snippets.

@nanaya
Created May 27, 2014 13:18
Show Gist options
  • Save nanaya/95bde0c601f3b50bf9d1 to your computer and use it in GitHub Desktop.
Save nanaya/95bde0c601f3b50bf9d1 to your computer and use it in GitHub Desktop.
#!/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
@nanaya
Copy link
Author

nanaya commented May 27, 2014

I thought ruby built-in function is faster. Except it's not - it's on par and slower to each loop on Fixnums and Floats respectively. And that sweet .reduce function? Slow, it seems.

### ruby 2.1.2, Ubuntu 14.04
>>> Mode: 100000 / Fixnum
Manual: 0
Reduce: 0
Native: 0
Rehearsal ------------------------------------------
manual   5.120000   0.010000   5.130000 (  5.129201)
reduce   8.010000   0.030000   8.040000 (  8.049760)
native   5.180000   0.010000   5.190000 (  5.195847)
-------------------------------- total: 18.360000sec

             user     system      total        real
manual   5.090000   0.030000   5.120000 (  5.119609)
reduce   7.630000   0.020000   7.650000 (  7.653785)
native   5.190000   0.030000   5.220000 (  5.222586)

>>> Mode: 100000 / Float
Manual: 3.061265161730109e-05
Reduce: 3.061265161730109e-05
Native: 3.061265161730109e-05
Rehearsal ------------------------------------------
manual   5.690000   0.010000   5.700000 (  5.713962)
reduce   8.470000   0.010000   8.480000 (  8.487023)
native   6.450000   0.010000   6.460000 (  6.471463)
-------------------------------- total: 20.640000sec

             user     system      total        real
manual   5.730000   0.010000   5.740000 (  5.744951)
reduce   8.560000   0.040000   8.600000 (  8.604024)
native   6.510000   0.010000   6.520000 (  6.523319)

>>> Mode: 200000 / Fixnum
Manual: 0
Reduce: 0
Native: 0
Rehearsal ------------------------------------------
manual  10.000000   0.010000  10.010000 ( 10.022561)
reduce  15.890000   0.040000  15.930000 ( 15.931561)
native  10.560000   0.040000  10.600000 ( 10.620045)
-------------------------------- total: 36.540000sec

             user     system      total        real
manual  10.470000   0.070000  10.540000 ( 10.540247)
reduce  15.270000   0.020000  15.290000 ( 15.312281)
native  10.440000   0.050000  10.490000 ( 10.496306)

>>> Mode: 200000 / Float
Manual: 6.852365395104698e-07
Reduce: 6.852365395104698e-07
Native: 6.852365395104698e-07
Rehearsal ------------------------------------------
manual  11.460000   0.080000  11.540000 ( 11.541505)
reduce  16.980000   0.070000  17.050000 ( 17.055315)
native  12.980000   0.040000  13.020000 ( 13.026320)
-------------------------------- total: 41.610000sec

             user     system      total        real
manual  11.740000   0.100000  11.840000 ( 11.851814)
reduce  16.720000   0.070000  16.790000 ( 16.795347)
native  13.150000   0.080000  13.230000 ( 13.229483)

>>> Mode: 500000 / Fixnum
Manual: 0
Reduce: 0
Native: 0
Rehearsal ------------------------------------------
manual  25.250000   0.070000  25.320000 ( 25.323536)
reduce  40.330000   0.220000  40.550000 ( 40.564729)
native  26.770000   0.120000  26.890000 ( 26.901012)
-------------------------------- total: 92.760000sec

             user     system      total        real
manual  26.620000   0.150000  26.770000 ( 26.779559)
reduce  38.920000   0.230000  39.150000 ( 39.152389)
native  26.520000   0.160000  26.680000 ( 26.683037)

>>> Mode: 500000 / Float
Manual: 3.302680244843259e-07
Reduce: 3.302680244843259e-07
Native: 3.302680244843259e-07
Rehearsal ------------------------------------------
manual  29.040000   0.150000  29.190000 ( 29.193569)
reduce  42.580000   0.110000  42.690000 ( 42.703462)
native  32.830000   0.050000  32.880000 ( 32.887886)
------------------------------- total: 104.760000sec

             user     system      total        real
manual  29.340000   0.050000  29.390000 ( 29.386205)
reduce  42.060000   0.070000  42.130000 ( 42.138832)
native  32.940000   0.100000  33.040000 ( 33.038606)

>>> Mode: 1000000 / Fixnum
Manual: 1
Reduce: 1
Native: 1
Rehearsal ------------------------------------------
manual  51.080000   0.110000  51.190000 ( 51.196595)
reduce  79.840000   0.170000  80.010000 ( 80.001240)
native  53.370000   0.150000  53.520000 ( 53.531409)
------------------------------- total: 184.720000sec

             user     system      total        real
manual  52.800000   0.100000  52.900000 ( 52.907837)
reduce  77.970000   0.190000  78.160000 ( 78.158417)
native  52.880000   0.090000  52.970000 ( 52.978024)

>>> Mode: 1000000 / Float
Manual: 4.806538163037999e-07
Reduce: 4.806538163037999e-07
Native: 4.806538163037999e-07
Rehearsal ------------------------------------------
manual  57.760000   0.110000  57.870000 ( 57.867156)
reduce  85.470000   0.190000  85.660000 ( 85.684138)
native  66.080000   0.220000  66.300000 ( 66.303009)
------------------------------- total: 209.830000sec

             user     system      total        real
manual  58.960000   0.140000  59.100000 ( 59.104052)
reduce  84.100000   0.180000  84.280000 ( 84.289455)
native  65.750000   0.180000  65.930000 ( 65.920207)

@nanaya
Copy link
Author

nanaya commented May 27, 2014

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