Skip to content

Instantly share code, notes, and snippets.

@colinsurprenant
Last active August 29, 2015 14:06
Show Gist options
  • Save colinsurprenant/09d93cbddb8f8e6daa99 to your computer and use it in GitHub Desktop.
Save colinsurprenant/09d93cbddb8f8e6daa99 to your computer and use it in GitHub Desktop.
Ruby JRuby tight loop performance
require "thread"
require "benchmark"
COUNT = 100000000
REFRESH_COUNT = 100
# basic looping bench
Benchmark.bmbm(40) do |bm|
bm.report("while true") do
n = 0
while true
n += 1
break if n >= COUNT
end
end
end
Benchmark.bmbm(40) do |bm|
bm.report("loop do") do
n = 0
loop do
n += 1
break if n >= COUNT
end
end
end
Benchmark.bmbm(40) do |bm|
bm.report("(1..COUNT)") do
n = 0
(1..COUNT).each{|i| n += 1}
end
end
Benchmark.bmbm(40) do |bm|
bm.report("COUNT.times") do
n = 0
COUNT.times.each{|i| n += 1}
end
end
# loop with a mutex synchronize at every cycle
Benchmark.bmbm(40) do |bm|
mutex = Mutex.new
bm.report("while true + always mutex") do
i = 0
n = 0
while true
n += 1
mutex.synchronize{i = n}
break if n >= COUNT
end
end
end
# loop with a mutex synchronize at every REFRESH_COUNT (100) cycle
Benchmark.bmbm(40) do |bm|
mutex = Mutex.new
bm.report("while true + partial mutex") do
i = 0
n = 0
while true
n += 1
mutex.synchronize{i = n} if (n % REFRESH_COUNT) == 0
break if n >= COUNT
end
end
end
# loop with a mutex synchronize at every REFRESH_COUNT (100) cycle plus a separate thread compiling stats
stats = []
Benchmark.bmbm(40) do |bm|
bm.report("while true + partial mutex + stats thread") do
mutex = Mutex.new
i = 0
stats = []
t = Thread.new do
loop do
start = mutex.synchronize{i}
sleep(1)
mutex.synchronize{stats << (i - start)}
end
end
n = 0
while true
n += 1
mutex.synchronize{i = n} if (n % REFRESH_COUNT) == 0
break if n >= COUNT
end
mutex.synchronize{t.kill}
end
end
puts(stats.inspect)
jruby 1.7.15 (1.9.3p392) 2014-09-03 82b5cc3 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_11-b21 +jit [darwin-x86_64]
Rehearsal ----------------------------------------------------------------------------
while true                                 2.710000   0.050000   2.760000 (  2.514000)
------------------------------------------------------------------- total: 2.760000sec

                                               user     system      total        real
while true                                 2.140000   0.030000   2.170000 (  2.253000)
Rehearsal ----------------------------------------------------------------------------
loop do                                    5.350000   0.040000   5.390000 (  5.419000)
------------------------------------------------------------------- total: 5.390000sec

                                               user     system      total        real
loop do                                    5.110000   0.040000   5.150000 (  5.322000)
Rehearsal ----------------------------------------------------------------------------
(1..COUNT)                                 5.180000   0.070000   5.250000 (  5.397000)
------------------------------------------------------------------- total: 5.250000sec

                                               user     system      total        real
(1..COUNT)                                 5.280000   0.070000   5.350000 (  5.474000)
Rehearsal ----------------------------------------------------------------------------
COUNT.times                                5.830000   0.080000   5.910000 (  6.118000)
------------------------------------------------------------------- total: 5.910000sec

                                               user     system      total        real
COUNT.times                                5.930000   0.070000   6.000000 (  6.213000)
Rehearsal ----------------------------------------------------------------------------
while true + always mutex                 18.090000   0.150000  18.240000 ( 18.785000)
------------------------------------------------------------------ total: 18.240000sec

                                               user     system      total        real
while true + always mutex                 17.790000   0.150000  17.940000 ( 18.569000)
Rehearsal ----------------------------------------------------------------------------
while true + partial mutex                 5.390000   0.040000   5.430000 (  5.508000)
------------------------------------------------------------------- total: 5.430000sec

                                               user     system      total        real
while true + partial mutex                 5.590000   0.040000   5.630000 (  5.737000)
Rehearsal -----------------------------------------------------------------------------
while true + partial mutex + stats thread   5.700000   0.040000   5.740000 (  5.862000)
-------------------------------------------------------------------- total: 5.740000sec

                                                user     system      total        real
while true + partial mutex + stats thread   5.570000   0.040000   5.610000 (  5.742000)
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
Rehearsal ----------------------------------------------------------------------------
while true                                 2.110000   0.000000   2.110000 (  2.154727)
------------------------------------------------------------------- total: 2.110000sec

                                               user     system      total        real
while true                                 2.070000   0.000000   2.070000 (  2.163198)
Rehearsal ----------------------------------------------------------------------------
loop do                                    5.720000   0.010000   5.730000 (  5.925485)
------------------------------------------------------------------- total: 5.730000sec

                                               user     system      total        real
loop do                                    5.920000   0.000000   5.920000 (  6.127044)
Rehearsal ----------------------------------------------------------------------------
(1..COUNT)                                 5.600000   0.010000   5.610000 (  5.807693)
------------------------------------------------------------------- total: 5.610000sec

                                               user     system      total        real
(1..COUNT)                                 5.200000   0.010000   5.210000 (  5.391842)
Rehearsal ----------------------------------------------------------------------------
COUNT.times                                5.290000   0.000000   5.290000 (  5.444737)
------------------------------------------------------------------- total: 5.290000sec

                                               user     system      total        real
COUNT.times                                5.380000   0.010000   5.390000 (  5.549190)
Rehearsal ----------------------------------------------------------------------------
while true + always mutex                 22.690000   0.020000  22.710000 ( 23.521842)
------------------------------------------------------------------ total: 22.710000sec

                                               user     system      total        real
while true + always mutex                 21.790000   0.030000  21.820000 ( 22.654493)
Rehearsal ----------------------------------------------------------------------------
while true + partial mutex                 5.070000   0.000000   5.070000 (  5.228915)
------------------------------------------------------------------- total: 5.070000sec

                                               user     system      total        real
while true + partial mutex                 4.890000   0.010000   4.900000 (  5.054542)
Rehearsal -----------------------------------------------------------------------------
while true + partial mutex + stats thread   5.140000   0.010000   5.150000 (  5.341653)
-------------------------------------------------------------------- total: 5.150000sec

                                                user     system      total        real
while true + partial mutex + stats thread   4.900000   0.000000   4.900000 (  5.094698)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment