Strange performance results ruby / jruby

  • each is faster in ruby- inject is fast in jruby- inject is much
    slower than each in ruby (and jruby)
    example:

require ‘benchmark’
def sum_upto_inject(nr) (1…nr).inject(0) { |sum,n| sum+n }end
def sum_upto_each(nr) sum = 0 (1…nr).each { |n| sum += n } sumend
def bm bm = Benchmark.measure do n = 4000 (1…n).each { |nr|
yield(nr) } end puts bmend
bm { |nr| sum_upto_each(nr) }bm { |nr| sum_upto_inject(nr) }

ruby 1.8.6# 4.437000 0.000000 4.437000 ( 4.453000)# 15.172000

0.015000 15.187000 ( 15.266000)

jruby 1.1.3# 4.938000 0.000000 4.938000 ( 4.933815)# 6.234000

0.000000 6.234000 ( 6.223913)


Windows 7: helpt je meer voor elkaar te krijgen. Ontdek Windows 7.
Windows help & learning

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Feb 2, 2010, at 3:07 PM, ADG ADG wrote:

jruby 1.1.3# 4.938000 0.000000 4.938000 ( 4.933815)# 6.234000 0.000000 6.234000 ( 6.223913)

Inject is about twice as slow as using #each.

  1. Upgrade to a newer jruby. Release 1.4.0 has been available for months
    and 1.5.0-dev is stable.

  2. Use more than 4000 iterations to benchmark anything with jruby. It
    gets penalized for taking longer to startup. Also, the JVM needs more
    iterations for the really smart optimizations to kick in via HotSpot.

Here is your bench redone.

require ‘benchmark’
def sum_upto_inject(nr)
(1…nr).inject(0) { |sum,n| sum+n }
end
def sum_upto_each(nr)
sum = 0
(1…nr).each { |n| sum += n }
sum
end

def bm
bm = Benchmark.measure do
n = 4000
(1…n).each { |nr| yield(nr) }
end
puts bm
end
bm { |nr| sum_upto_each(nr) }
bm { |nr| sum_upto_inject(nr) }

ruby 1.8.7 patch 72

cremes$ ruby t.rb
49.300000 0.010000 49.310000 ( 49.317057)
113.600000 0.040000 113.640000 (113.645203)

jruby 1.5.0-dev from today

cremes$ jruby --server t.rb
18.094000 0.000000 18.094000 ( 18.048000)
34.181000 0.000000 34.181000 ( 34.181000)

rubinius 1.0.0-rc2 from today with JIT enabled

cremes$ rbx t.rb
11.883685 0.000000 11.883685 ( 11.883699)
77.520037 0.000000 77.520037 ( 77.520084)

rubinius 1.0.0-rc2 from today with JIT disabled (interpreted)

cremes$ rbx -Xint t.rb
25.975083 0.000000 25.975083 ( 25.975111)
103.910543 0.000000 103.910543 (103.910585)

As you can clearly see, #inject is only about twice as slow under JRuby.
It is significantly slower under MRI.

cr


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Also note that we have a faster implementation of inject for actual
Arrays (and we could optimize Range similiarly). Try this version of
sum_upto_inject(nr):

def sum_upto_inject(nr)
(1…nr).to_a.inject(0) { |sum,n| sum+n }
end

You’ll see that inject is much closer to each performance for an Array.

-Tom

On Tue, Feb 2, 2010 at 3:38 PM, Chuck R. [email protected]
wrote:

def bm bm = Benchmark.measure do n = 4000 (1…n).each { |nr| yield(nr) } end puts bmend

end

rubinius 1.0.0-rc2 from today with JIT enabled

As you can clearly see, #inject is only about twice as slow under JRuby. It is significantly slower under MRI.


blog: http://blog.enebo.com twitter: tom_enebo
mail: [email protected]


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

And of course this reports only a single run; if you simply wrap the
two bm calls in “5.times { }” the subsequent iterations are anywhere
from 15-25% faster.

Ultimately, this bench is largely measuring block dispatch performance
more than anything else, and it’s a known issue in JRuby that blocks
receiving 2 or more arguments currently have a lot more overhead than
blocks receiving one argument. So inject here is paying a higher
penalty simply because it takes two arguments. We plan to fix it, but
it hasn’t been a high priority up to now.

  • Charlie

On Tue, Feb 2, 2010 at 4:47 PM, Thomas E Enebo [email protected]
wrote:

-Tom

require ‘benchmark’

  1. Upgrade to a newer jruby. Release 1.4.0 has been available for months and 1.5.0-dev is stable.
    Â sum = 0
    end
    Â 18.094000 Â 0.000000 Â 18.094000 ( 18.048000)
    103.910543 Â 0.000000 103.910543 (103.910585)
    Â Â http://xircles.codehaus.org/manage_email

To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email