Ruby performance

Hi,

I have snippet from my project, and this iteration is performing very
very badly compared to same implementation in Java

Java : Time Elapsed in milliseconds: 8109 ms.
Ruby : Time Elapsed in Seconds: 132.28125 sec.

It a huge difference, Am i missing something here, is there any
particular best practices. I am trying to implement Ruby for a
particular project, where it needs to read/process huge arrays etc
Please guide me.

input = "ababaa" * 10000
suffix_array = input.split(//)
suffix_array_len = suffix_array.size

for i in 1..suffix_array_len-1 do
  total=0
  for j in i..suffix_array_len-1 do

  end
end

···

--
Posted via http://www.ruby-forum.com/.

maybe try jruby in --server mode?

···

--
Posted via http://www.ruby-forum.com/.

replace "for i in 1..suffix_array_len-1 do"
with "suffix_array.each_with_index do |s,i|"

it may faster depending what you want

···

--
Posted via http://www.ruby-forum.com/.

The MRI is, unfortunately, slow as balls, and there's not much you can
do. For computation-intensive tasks try JRuby or, well, a different
programmming language.

-- Matma Rex

Maybe if you told us what that code was supposed to do then we could suggest a better approach.

Henry

···

On 30/06/2012, at 5:13 AM, anaray anaray wrote:

Hi,

I have snippet from my project, and this iteration is performing very
very badly compared to same implementation in Java

Hi,

I have snippet from my project, and this iteration is performing very
very badly compared to same implementation in Java

Java : Time Elapsed in milliseconds: 8109 ms.
Ruby : Time Elapsed in Seconds: 132.28125 sec.

It a huge difference, Am i missing something here, is there any
particular best practices.

Yes that's a huge difference, and what you're seeing is roughly what you have to expect. Count on 10-20 times slower unless you are heavily reliant on stuff Java can't do well (like start up times, or fall back on underlying C code as some gems do). Ruby can also be a big help if you have memory constraints.

You might consider JRuby because it sounds as though the JVM is something you're familiar with, but also because you can take advantage of multi-core architectures if your algorithms are parallelisable. But it's a trade off in that some gems won't work in JRuby.

You might also consider coding parts of your program in C. Yeah, I know. This is probably the *last* thing you want to do :slight_smile:

I am trying to implement Ruby for a
particular project, where it needs to read/process huge arrays etc
Please guide me.

In general there's not much more to say. But if you get into specifics, maybe. However, most of the suggestions we could make will apply to a Java version as well, so you aren't necessarily going to see a relative performance increase, maybe, if lucky an absolute performance increase.

input = "ababaa" * 10000
suffix_array = input.split(//)

Is that really what you mean?

suffix_array_len = suffix_array.size

for i in 1..suffix_array_len-1 do

Are you starting at 1 for a reason? The first index is 0.

I'd probably write this something like:

(0...l).each do | i |

The '0...l' is the same as 0..(l-1)

total=0
for j in i..suffix_array_len-1 do

end
end

Cheers,
Bob

···

On 2012-06-29, at 1:13 PM, anaray anaray wrote:

--
Posted via http://www.ruby-forum.com/\.

----
Bob Hutchison
Recursive Design Inc.
http://www.recursive.ca/
weblog: http://xampl.com/so

Hi,

If performance is what you're after, I think Ruby is simply the wrong
language.

You can try different Ruby implementations, you can optimize your code a
bit, but you'll never come close to the 8 seconds of Java.

I'm not even sure if you've actually adapted Ruby, because the code
above rather looks like you're translating Java code into Ruby.

···

--
Posted via http://www.ruby-forum.com/.

Whoa -- excuse me. I try to run some quick numbers out of curiosity and
I get slammed. I didn't realize this forum met the standards of
peer-review literature in its content.

···

--
Posted via http://www.ruby-forum.com/.

Bartosz Dziewoński wrote in post #1066673:

The MRI is, unfortunately, slow as balls, and there's not much you can
do. For computation-intensive tasks try JRuby or, well, a different
programmming language.

-- Matma Rex

if you write shitty code, its still shit in jruby too.
MRI 1.9 is faster then 1.8.

and i prefer MRI over JRuby because i need C-Gems

···

--
Posted via http://www.ruby-forum.com/\.

Thanks Bob for your valuable input. I think then in this case, Ruby with
MRI is a wrong choice. This was just preliminary test, my work involves
lots String, array processing, which involves huge data-set.

Recently I tried Ruby, and really loved the language, so i thought of
using it in my next work.

I am going to try Jruby

Thanks

···

--
Posted via http://www.ruby-forum.com/.

Yes, I am Ruby newbie :slight_smile:
Later after posting this, i tried the Ruby way of iteration, but it also
didn't help much, I am well short of requirement

Thanks

Jan E. wrote in post #1066782:

···

Hi,

If performance is what you're after, I think Ruby is simply the wrong
language.

You can try different Ruby implementations, you can optimize your code a
bit, but you'll never come close to the 8 seconds of Java.

I'm not even sure if you've actually adapted Ruby, because the code
above rather looks like you're translating Java code into Ruby.

--
Posted via http://www.ruby-forum.com/\.

Hey, excuse them.

···

On Wed, Jul 04, 2012 at 06:48:14AM +0900, Dan Connelly wrote:

Whoa -- excuse me.

--
---- WBR, Michael Shigorin <mike@altlinux.ru>
  ------ Linux.Kiev http://www.linux.kiev.ua/

Yes, of course 1.9 is faster.

Still. Benchmark this for me, would you:

    1_000_000_000.times{|a| }

It takes 103 seconds to run on my machine. The following code in C++,
compiled with -O0, takes four seconds.

int main()
{
  for(int i=0; i<1000000000; i++){};
  return 0;
}

(I have verified that the generated assemly code actually runs the loop.)

MRI Ruby *is* two orders of magnitude slower than C and often
noticeably slower than other interpreted languages and no matter how
much you love it, you can't deny it's slow.

-- Matma Rex

···

2012/6/29 Hans Mackowiak <lists@ruby-forum.com>:

Bartosz Dziewoński wrote in post #1066673:

The MRI is, unfortunately, slow as balls, and there's not much you can
do. For computation-intensive tasks try JRuby or, well, a different
programmming language.

-- Matma Rex

if you write shitty code, its still shit in jruby too.
MRI 1.9 is faster then 1.8.

Thanks Bob for your valuable input. I think then in this case, Ruby with
MRI is a wrong choice. This was just preliminary test, my work involves
lots String, array processing, which involves huge data-set.

Recently I tried Ruby, and really loved the language, so i thought of
using it in my next work.

I am going to try Jruby

If JRuby turns out to be fast enough for your requirements, I'd suggest developing as much as possible using MRI then moving it to JRuby. It'll be more pleasant. If you use gems make sure they work in JRuby, some won't.

Good Luck!

Cheers,
Bob

···

On 2012-07-01, at 1:02 AM, anaray anaray wrote:

Thanks

--
Posted via http://www.ruby-forum.com/\.

----
Bob Hutchison
Recursive Design Inc.
http://www.recursive.ca/
weblog: http://xampl.com/so

Can you be more specific about your use case? I am asking because
there might actually be other factors which dominate runtime for the
real code you want to execute. For example, if your data set is huge
and it needs to be read from some disk then there is a good chance
that your application is IO bound and hence looping performance does
not matter that much. Or you can work with different data structures
to get better results. Or you implement your core data structures in
a C extension and use Ruby for the business logic.

Kind regards

robert

···

On Sun, Jul 1, 2012 at 7:02 AM, anaray anaray <lists@ruby-forum.com> wrote:

Thanks Bob for your valuable input. I think then in this case, Ruby with
MRI is a wrong choice. This was just preliminary test, my work involves
lots String, array processing, which involves huge data-set.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Yes, of course 1.9 is faster.

Still. Benchmark this for me, would you:

1_000_000_000.times{|a| }

It takes 103 seconds to run on my machine. The following code in C++,
compiled with -O0, takes four seconds.

int main()
{
for(int i=0; i<1000000000; i++){};
return 0;
}

(I have verified that the generated assemly code actually runs the loop.)

MRI Ruby *is* two orders of magnitude slower than C and often
noticeably slower than other interpreted languages and no matter how
much you love it, you can't deny it's slow.

-- Matma Rex

···

2012/6/29 Hans Mackowiak <lists@ruby-forum.com>:

MRI 1.9 is faster then 1.8.

Bartosz Dziewoński wrote in post #1066681:

Still. Benchmark this for me, would you:

1_000_000_000.times{|a| }

It takes 103 seconds to run on my machine. The following code in C++,
compiled with -O0, takes four seconds.

int main()
{
for(int i=0; i<1000000000; i++){};
return 0;
}

A loop with no payload is a completely useless benchmarking scenario.

···

--
Posted via http://www.ruby-forum.com/\.

You are wrong. It's the same as benchmarking a loop with a payload,
without benchmarking the payload. You could also consider the payload
to be integer incrementation.

-- Matma Rex

···

2012/7/1 Andreas S. <lists@ruby-forum.com>:

A loop with no payload is a completely useless benchmarking scenario.

I am going to place myself right between your chairs. I think both of
you have it wrong. :-))

A loop without payload is not completely useless - but it only tells
you about a very artificial test case. If the payload is more
expensive than the looping and both languages have less differences
there (e.g. because logic is IO bound) then the looping test does not
really help much for the real case. Similarly if the real code does
not involve looping at all because other algorithms are chosen. :slight_smile:

Kind regards

robert

···

On Sun, Jul 1, 2012 at 1:42 PM, Bartosz Dziewoński <matma.rex@gmail.com> wrote:

2012/7/1 Andreas S. <lists@ruby-forum.com>:

A loop with no payload is a completely useless benchmarking scenario.

You are wrong. It's the same as benchmarking a loop with a payload,
without benchmarking the payload. You could also consider the payload
to be integer incrementation.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Here's my contribution:

Ruby:
i = 0
10_000_000.times { i += 1 }
puts i

Perl:
for my $n ( 0 .. 1e7 - 1) {
  $i ++
}
print "$i\n"

Result:
Ruby: 2.970 seconds
Perl: 0.682 seconds

Wow...

···

--
Posted via http://www.ruby-forum.com/.