Skip to content

Instantly share code, notes, and snippets.

@headius
Created June 10, 2013 12:07
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 headius/5748268 to your computer and use it in GitHub Desktop.
Save headius/5748268 to your computer and use it in GitHub Desktop.
Benchmarking kind_of?
require 'benchmark'
class A; end
class B < A; end
class C < B; end
class D < C; end
class E < D; end
class MyString < String; end
e = E.new
m = MyString.new
puts "10m kind_of? on a five-class hierarchy"
10.times {
puts Benchmark.measure {
i = 0
while i < 10000000
e.kind_of? A
i += 1
end
}
}
puts "10m kind_of on a subclass of String"
10.times {
puts Benchmark.measure {
i = 0
while i < 10000000
m.kind_of? String
i += 1
end
}
}
puts "10m * 10 failed kind_of against Fixnum"
10.times {
puts Benchmark.measure {
i = 0
while i < 10000000
i.kind_of? String
i.kind_of? Array
i.kind_of? Time
i.kind_of? Hash
i.kind_of? Class
i.kind_of? Module
i.kind_of? Bignum
i.kind_of? Float
i.kind_of? Binding
i.kind_of? NilClass
i += 1
end
}
}
BEFORE, Java 6
10m kind_of? on a five-class hierarchy
0.540000 0.020000 0.560000 ( 0.409000)
0.350000 0.020000 0.370000 ( 0.313000)
0.330000 0.020000 0.350000 ( 0.326000)
0.330000 0.020000 0.350000 ( 0.326000)
0.330000 0.010000 0.340000 ( 0.322000)
0.330000 0.020000 0.350000 ( 0.325000)
0.320000 0.010000 0.330000 ( 0.324000)
0.330000 0.020000 0.350000 ( 0.329000)
0.330000 0.020000 0.350000 ( 0.328000)
0.330000 0.020000 0.350000 ( 0.325000)
10m kind_of on a subclass of String
0.460000 0.010000 0.470000 ( 0.410000)
0.380000 0.020000 0.400000 ( 0.360000)
0.300000 0.020000 0.320000 ( 0.303000)
0.300000 0.010000 0.310000 ( 0.297000)
0.300000 0.020000 0.320000 ( 0.298000)
0.310000 0.020000 0.330000 ( 0.299000)
0.300000 0.010000 0.310000 ( 0.296000)
0.300000 0.020000 0.320000 ( 0.296000)
0.300000 0.020000 0.320000 ( 0.294000)
0.310000 0.010000 0.320000 ( 0.297000)
10m * 10 failed kind_of against Fixnum
3.120000 0.030000 3.150000 ( 2.920000)
2.960000 0.020000 2.980000 ( 2.865000)
2.310000 0.020000 2.330000 ( 2.279000)
2.300000 0.010000 2.310000 ( 2.293000)
2.310000 0.020000 2.330000 ( 2.305000)
2.410000 0.030000 2.440000 ( 2.407000)
2.260000 0.020000 2.280000 ( 2.265000)
2.210000 0.020000 2.230000 ( 2.215000)
2.210000 0.010000 2.220000 ( 2.213000)
2.230000 0.020000 2.250000 ( 2.222000)
AFTER, Java 6
10m kind_of? on a five-class hierarchy
0.600000 0.030000 0.630000 ( 0.435000)
0.370000 0.010000 0.380000 ( 0.338000)
0.360000 0.020000 0.380000 ( 0.346000)
0.350000 0.020000 0.370000 ( 0.348000)
0.350000 0.010000 0.360000 ( 0.344000)
0.360000 0.020000 0.380000 ( 0.351000)
0.350000 0.020000 0.370000 ( 0.345000)
0.350000 0.010000 0.360000 ( 0.347000)
0.350000 0.020000 0.370000 ( 0.345000)
0.360000 0.020000 0.380000 ( 0.352000)
10m kind_of on a subclass of String
0.460000 0.020000 0.480000 ( 0.400000)
0.370000 0.010000 0.380000 ( 0.346000)
0.340000 0.020000 0.360000 ( 0.330000)
0.320000 0.010000 0.330000 ( 0.321000)
0.330000 0.020000 0.350000 ( 0.318000)
0.320000 0.020000 0.340000 ( 0.319000)
0.330000 0.010000 0.340000 ( 0.322000)
0.320000 0.020000 0.340000 ( 0.320000)
0.330000 0.020000 0.350000 ( 0.327000)
0.330000 0.010000 0.340000 ( 0.327000)
10m * 10 failed kind_of against Fixnum
2.280000 0.020000 2.300000 ( 2.112000)
2.040000 0.020000 2.060000 ( 1.991000)
2.010000 0.020000 2.030000 ( 1.980000)
1.970000 0.020000 1.990000 ( 1.968000)
1.980000 0.020000 2.000000 ( 1.974000)
2.000000 0.010000 2.010000 ( 1.991000)
1.980000 0.020000 2.000000 ( 1.971000)
1.960000 0.020000 1.980000 ( 1.964000)
1.960000 0.020000 1.980000 ( 1.951000)
1.960000 0.010000 1.970000 ( 1.960000)
BEFORE, Java 7u40 (+indy)
10m kind_of? on a five-class hierarchy
0.890000 0.050000 0.940000 ( 0.468000)
0.330000 0.020000 0.350000 ( 0.208000)
0.190000 0.000000 0.190000 ( 0.184000)
0.190000 0.000000 0.190000 ( 0.182000)
0.190000 0.000000 0.190000 ( 0.184000)
0.190000 0.000000 0.190000 ( 0.182000)
0.180000 0.010000 0.190000 ( 0.182000)
0.190000 0.000000 0.190000 ( 0.184000)
0.190000 0.000000 0.190000 ( 0.186000)
0.190000 0.000000 0.190000 ( 0.189000)
10m kind_of on a subclass of String
0.290000 0.000000 0.290000 ( 0.237000)
0.160000 0.000000 0.160000 ( 0.146000)
0.150000 0.000000 0.150000 ( 0.146000)
0.140000 0.010000 0.150000 ( 0.145000)
0.150000 0.000000 0.150000 ( 0.149000)
0.150000 0.000000 0.150000 ( 0.147000)
0.150000 0.000000 0.150000 ( 0.143000)
0.140000 0.000000 0.140000 ( 0.142000)
0.150000 0.000000 0.150000 ( 0.143000)
0.150000 0.000000 0.150000 ( 0.143000)
10m * 10 failed kind_of against Fixnum
1.630000 0.010000 1.640000 ( 1.389000)
1.190000 0.000000 1.190000 ( 1.101000)
1.060000 0.000000 1.060000 ( 1.031000)
1.030000 0.010000 1.040000 ( 1.021000)
1.030000 0.000000 1.030000 ( 1.025000)
1.030000 0.000000 1.030000 ( 1.022000)
1.020000 0.000000 1.020000 ( 1.020000)
1.030000 0.000000 1.030000 ( 1.029000)
1.050000 0.010000 1.060000 ( 1.033000)
1.050000 0.000000 1.050000 ( 1.025000)
AFTER, Java 7u40 (+indy)
10m kind_of? on a five-class hierarchy
0.920000 0.060000 0.980000 ( 0.476000)
0.320000 0.010000 0.330000 ( 0.209000)
0.180000 0.010000 0.190000 ( 0.182000)
0.190000 0.000000 0.190000 ( 0.185000)
0.190000 0.000000 0.190000 ( 0.185000)
0.190000 0.000000 0.190000 ( 0.182000)
0.190000 0.000000 0.190000 ( 0.184000)
0.180000 0.000000 0.180000 ( 0.181000)
0.190000 0.010000 0.200000 ( 0.183000)
0.180000 0.000000 0.180000 ( 0.183000)
10m kind_of on a subclass of String
0.310000 0.000000 0.310000 ( 0.239000)
0.160000 0.000000 0.160000 ( 0.136000)
0.130000 0.000000 0.130000 ( 0.126000)
0.130000 0.000000 0.130000 ( 0.127000)
0.130000 0.000000 0.130000 ( 0.125000)
0.130000 0.010000 0.140000 ( 0.129000)
0.140000 0.000000 0.140000 ( 0.129000)
0.130000 0.000000 0.130000 ( 0.125000)
0.130000 0.000000 0.130000 ( 0.128000)
0.130000 0.000000 0.130000 ( 0.126000)
10m * 10 failed kind_of against Fixnum
1.540000 0.010000 1.550000 ( 1.217000)
1.120000 0.010000 1.130000 ( 0.953000)
0.800000 0.000000 0.800000 ( 0.774000)
0.790000 0.000000 0.790000 ( 0.789000)
0.790000 0.010000 0.800000 ( 0.786000)
0.790000 0.000000 0.790000 ( 0.780000)
0.790000 0.000000 0.790000 ( 0.783000)
0.780000 0.000000 0.780000 ( 0.780000)
0.800000 0.000000 0.800000 ( 0.787000)
0.780000 0.010000 0.790000 ( 0.782000)
@fred
Copy link

fred commented Jun 19, 2013

jruby on OpenJDK 1.8 is twice as fast as 1.7

java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

10m kind_of? on a five-class hierarchy
  1.010000   0.070000   1.080000 (  0.918000)
  0.520000   0.000000   0.520000 (  0.490000)
  0.550000   0.010000   0.560000 (  0.595000)
  0.550000   0.000000   0.550000 (  0.613000)
  0.550000   0.010000   0.560000 (  0.593000)
  0.540000   0.000000   0.540000 (  0.566000)
  0.540000   0.010000   0.550000 (  0.561000)
  0.540000   0.000000   0.540000 (  0.550000)
  0.540000   0.000000   0.540000 (  0.557000)
  0.550000   0.000000   0.550000 (  0.556000)
10m kind_of on a subclass of String
  0.700000   0.000000   0.700000 (  0.627000)
  0.580000   0.000000   0.580000 (  0.542000)
  0.490000   0.010000   0.500000 (  0.481000)
  0.490000   0.000000   0.490000 (  0.497000)
  0.490000   0.000000   0.490000 (  0.478000)
  0.480000   0.000000   0.480000 (  0.479000)
  0.490000   0.000000   0.490000 (  0.503000)
  0.490000   0.010000   0.500000 (  0.487000)
  0.500000   0.000000   0.500000 (  0.498000)
  0.510000   0.000000   0.510000 (  0.505000)
10m * 10 failed kind_of against Fixnum
  5.430000   0.030000   5.460000 (  5.180000)
  4.950000   0.010000   4.960000 (  4.812000)
  3.560000   0.000000   3.560000 (  3.642000)
  3.510000   0.000000   3.510000 (  3.510000)
  3.510000   0.010000   3.520000 (  3.508000)
  3.510000   0.000000   3.510000 (  3.513000)
  3.520000   0.010000   3.530000 (  3.525000)
  3.510000   0.000000   3.510000 (  3.498000)
  3.520000   0.000000   3.520000 (  3.520000)
  3.550000   0.010000   3.560000 (  3.605000)

openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-fred_2013_06_19_18_38-b00)
OpenJDK 64-Bit Server VM (build 25.0-b37, mixed mode)

10m kind_of? on a five-class hierarchy
  1.220000   0.070000   1.290000 (  0.919000)
  0.330000   0.010000   0.340000 (  0.296000)
  0.300000   0.000000   0.300000 (  0.291000)
  0.310000   0.000000   0.310000 (  0.310000)
  0.310000   0.010000   0.320000 (  0.299000)
  0.300000   0.000000   0.300000 (  0.296000)
  0.300000   0.000000   0.300000 (  0.293000)
  0.300000   0.000000   0.300000 (  0.306000)
  0.310000   0.000000   0.310000 (  0.301000)
  0.300000   0.010000   0.310000 (  0.298000)
10m kind_of on a subclass of String
  0.480000   0.000000   0.480000 (  0.419000)
  0.300000   0.000000   0.300000 (  0.262000)
  0.260000   0.000000   0.260000 (  0.253000)
  0.260000   0.010000   0.270000 (  0.256000)
  0.260000   0.000000   0.260000 (  0.257000)
  0.260000   0.000000   0.260000 (  0.254000)
  0.260000   0.000000   0.260000 (  0.264000)
  0.260000   0.000000   0.260000 (  0.276000)
  0.260000   0.010000   0.270000 (  0.254000)
  0.270000   0.000000   0.270000 (  0.274000)
10m * 10 failed kind_of against Fixnum
  2.520000   0.010000   2.530000 (  2.249000)
  1.950000   0.010000   1.960000 (  1.884000)
  1.680000   0.000000   1.680000 (  1.669000)
  1.630000   0.000000   1.630000 (  1.622000)
  1.630000   0.000000   1.630000 (  1.620000)
  1.630000   0.010000   1.640000 (  1.623000)
  1.630000   0.000000   1.630000 (  1.620000)
  1.630000   0.000000   1.630000 (  1.624000)
  1.650000   0.000000   1.650000 (  1.661000)
  1.630000   0.010000   1.640000 (  1.635000)

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