Skip to content

Instantly share code, notes, and snippets.

@kares
Last active June 6, 2019 10:22
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 kares/7188d075d614ed023ccad31cf6d968bb to your computer and use it in GitHub Desktop.
Save kares/7188d075d614ed023ccad31cf6d968bb to your computer and use it in GitHub Desktop.
require 'benchmark'
TIMES = (ENV['TIMES'] || 200_000_000).to_i
AS = ENV['AS'] == 'true'
require 'active_support/all' if AS
require 'active_support/core_ext' if AS
if ENV['REOPEN'] == 'true'
class Fixnum
def foo; 1+0 end
end
class Float
def bar; 1.0 end
end
end
Benchmark.bmbm do |x|
a = 1; b = 2
x.report("(1 + 12) + (99 * 2) [#{TIMES}x]") do
TIMES.times do
(a + 12) + (99 * b)
end
end
n = 2
x.report("2 + 1 [#{TIMES}x]") do
TIMES.times { n + 1 }
end
ni = 120
x.report("120 / 4 [#{TIMES}x]") do
TIMES.times { ni / 4 }
end
nf = 120.0
x.report("120.0 / 4.0 [#{TIMES}x]") do
TIMES.times { nf / 4.0 }
end
end
$ AS=true REOPEN=true JRUBY_OPTS="-Xcompile.invokedynamic=false" jruby benchmark.rb
Rehearsal --------------------------------------------------------------------
(1 + 12) + (99 * 2) [200000000x] 8.070000 0.000000 8.070000 ( 7.896033)
2 + 1 [200000000x] 5.260000 0.000000 5.260000 ( 5.111049)
120 / 4 [200000000x] 6.690000 0.010000 6.700000 ( 6.637188)
120.0 / 4.0 [200000000x] 6.320000 0.190000 6.510000 ( 6.180048)
---------------------------------------------------------- total: 26.540000sec
user system total real
(1 + 12) + (99 * 2) [200000000x] 7.880000 0.000000 7.880000 ( 7.877193)
2 + 1 [200000000x] 4.850000 0.000000 4.850000 ( 4.843902)
120 / 4 [200000000x] 6.480000 0.000000 6.480000 ( 6.474418)
120.0 / 4.0 [200000000x] 5.870000 0.050000 5.920000 ( 5.847652)
$ AS=true REOPEN=true JRUBY_OPTS="-Xcompile.invokedynamic=true" jruby benchmark.rb
Rehearsal --------------------------------------------------------------------
(1 + 12) + (99 * 2) [200000000x] 7.140000 0.000000 7.140000 ( 6.366402)
2 + 1 [200000000x] 4.520000 0.000000 4.520000 ( 4.358943)
120 / 4 [200000000x] 6.470000 0.000000 6.470000 ( 6.432327)
120.0 / 4.0 [200000000x] 4.720000 0.150000 4.870000 ( 4.583619)
---------------------------------------------------------- total: 23.000000sec
user system total real
(1 + 12) + (99 * 2) [200000000x] 5.760000 0.000000 5.760000 ( 5.739212)
2 + 1 [200000000x] 4.270000 0.000000 4.270000 ( 4.267017)
120 / 4 [200000000x] 6.410000 0.000000 6.410000 ( 6.387627)
120.0 / 4.0 [200000000x] 4.540000 0.050000 4.590000 ( 4.491516)
$ AS=true REOPEN=true JRUBY_OPTS="-Xcompile.invokedynamic=false" bin/jruby benchmark.rb
Rehearsal --------------------------------------------------------------------
(1 + 12) + (99 * 2) [200000000x] 6.790000 0.000000 6.790000 ( 6.547216)
2 + 1 [200000000x] 3.520000 0.000000 3.520000 ( 3.469472)
120 / 4 [200000000x] 3.680000 0.000000 3.680000 ( 3.651878)
120.0 / 4.0 [200000000x] 4.860000 0.130000 4.990000 ( 4.568167)
---------------------------------------------------------- total: 18.980000sec
user system total real
(1 + 12) + (99 * 2) [200000000x] 6.880000 0.000000 6.880000 ( 6.880773) 12.6% better*
2 + 1 [200000000x] 3.550000 0.000000 3.550000 ( 3.551130) 26.7% better*
120 / 4 [200000000x] 3.650000 0.000000 3.650000 ( 3.643466) 43.7% better*
120.0 / 4.0 [200000000x] 4.030000 0.020000 4.050000 ( 3.983929) 31.8% better*
* compared to *result_base-indy=false.txt*
$ AS=true REOPEN=true JRUBY_OPTS="-Xcompile.invokedynamic=true" bin/jruby benchmark.rb
Rehearsal --------------------------------------------------------------------
(1 + 12) + (99 * 2) [200000000x] 4.800000 0.010000 4.810000 ( 4.457754)
2 + 1 [200000000x] 3.650000 0.000000 3.650000 ( 3.527968)
120 / 4 [200000000x] 3.500000 0.000000 3.500000 ( 3.445908)
120.0 / 4.0 [200000000x] 4.150000 0.170000 4.320000 ( 4.014289)
---------------------------------------------------------- total: 16.280000sec
user system total real
(1 + 12) + (99 * 2) [200000000x] 4.340000 0.010000 4.350000 ( 4.342414) 24.3% better*
2 + 1 [200000000x] 3.670000 0.000000 3.670000 ( 3.669858) 14.0% better*
120 / 4 [200000000x] 3.680000 0.000000 3.680000 ( 3.676402) 42.4% better*
120.0 / 4.0 [200000000x] 3.950000 0.020000 3.970000 ( 3.910669) 12.9% better*
* compared to *result_base-indy=true.txt*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment