Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Benchmark Mono - Ruby

Reply
Thread Tools

Benchmark Mono - Ruby

 
 
Michael Gebhart
Guest
Posts: n/a
 
      02-06-2005
Hi,

because of my interest in mono and ruby I have done a small benchmark.
These are the results:

Mono Code:

using System;

class Bench {
public static void Main() {
double d = 0;

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

Console.WriteLine(d);
}

Needs 10.8 Seconds.


In Ruby:

d=0
1000000000.times {
d = d + 1
}

puts d


Needs: 8 minutes and 20 seconds.


Does not look very good Is there a possibility to tune my ruby-program,
to be as fast as mono?

Greetings

Mike
 
Reply With Quote
 
 
 
 
Jamis Buck
Guest
Posts: n/a
 
      02-06-2005
On 12:05 Sun 06 Feb , Michael Gebhart wrote:
> Hi,
>
> because of my interest in mono and ruby I have done a small benchmark.
> These are the results:
>
> Mono Code:
>
> using System;
>
> class Bench {
> public static void Main() {
> double d = 0;
>
> for (int i = 0; i < 1000000000; i++) {
> d = d + i;
> }
>
> Console.WriteLine(d);
> }
>
> Needs 10.8 Seconds.
>
>
> In Ruby:
>
> d=0
> 1000000000.times {
> d = d + 1
> }
>
> puts d
>
>
> Needs: 8 minutes and 20 seconds.
>
>
> Does not look very good Is there a possibility to tune my ruby-program,
> to be as fast as mono?


At the risk of sounding defensive...

My first question when seeing benchmarks like this: how many
real-world programs have, as their performance bottleneck, a massive
loop that just increments numbers? I'm sure there are some--I don't
deny it--but is your application one of them?

If you want to compare performance between two programming languages
and execution environments, the first rule is to find a benchmark that
properly measures the performance of those languages in the correct
domain. Figure out what you want those languages to do, for real, and
write a simple test that does that thing. Then run your test.

Second rule: figure out what to measure. Is execution time the only
important factor? Or is memory consumption an issue as well? Network
traffic? Development time? Maintenence effort? Portability? Etc.

If execution time and memory consumption are both the most important,
you'd do well to abandon both Ruby and Mono and code in C. Or assembly
code.

You can *always* find a situation in which language X will
"outperform" language Y (for some definition of "outperform"). There
are things that Ruby does better than Mono, I'm sure (but not being
familiar with Mono, I'm not qualified to say what they might be).
Although, at a glance, it looks like the Mono sample took twice as
many lines of code as the Ruby sample...

Just some things to think about. Certainly continue your
investigations--please don't think I'm discouraging that--but make
sure you are testing the right things, and comparing the right
metrics.

- Jamis

--
Jamis Buck

http://jamis.jamisbuck.org
------------------------------
"I am Victor of Borge. You will be assimil-nine-ed."



 
Reply With Quote
 
 
 
 
Navindra Umanee
Guest
Posts: n/a
 
      02-06-2005
Michael Gebhart <> wrote:
> d=0
> 1000000000.times {
> d = d + 1
> }
>
> puts d


1000000000.times --> 1000000000 method calls to block
d = d + 1 --> 1000000000 method calls to d
puts d --> 2 method calls
-----------------------------------------------------
2000000002 method calls
================================================== ===

Well, it sucks, but I guess you might want to realise that you are
performing *at least* 2000000001 method calls in Ruby, probably *a
factor* more at the C-level implementation, using the above code.

I don't think you can really get around that, since even using a for
loop probably requires that you use a range which might involve more
method calls.

I guess that's the price of pervasive and flexible OO throughout the
language. I wonder if ruby2c might be able to optimise this sort of
thing.

How does Mono fare when performing 2000000002 method calls?

Cheers,
Navin.


 
Reply With Quote
 
Kent Sibilev
Guest
Posts: n/a
 
      02-06-2005
Consider this 'tuning':

$ cat t.rb
require 'ruby_to_c'

module Inline
class Ruby < Inline::C
def initialize(mod)
super
end

def optimize(meth)
src = RubyToC.translate(@mod, meth)
@mod.class_eval "alias :#{meth}_slow :#{meth}"
@mod.class_eval "remove_method :#{meth}"
c src
end
end
end

class Test
def run
d=0
1000000000.downto(1) {
d = d + 1
}
return d
end

inline(:Ruby) do |builder|
builder.optimize :run
end
end

puts Test.new.run

$ time ruby t.rb
1000000000

real 0m3.118s
user 0m2.670s
sys 0m0.050s

$ cat t.cs
using System;

class Bench {
public static void Main() {
double d = 0;

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

Console.WriteLine(d);
}
}

$ mcs t.cs
Compilation succeeded

$ time mono t.exe
4.99999999067109E+17

real 0m37.692s
user 0m34.540s
sys 0m0.040s


Cheers,
Kent.

On Feb 5, 2005, at 10:05 PM, Michael Gebhart wrote:

> Hi,
>
> because of my interest in mono and ruby I have done a small benchmark.
> These are the results:
>
> Mono Code:
>
> using System;
>
> class Bench {
> public static void Main() {
> double d = 0;
>
> for (int i = 0; i < 1000000000; i++) {
> d = d + i;
> }
>
> Console.WriteLine(d);
> }
>
> Needs 10.8 Seconds.
>
>
> In Ruby:
>
> d=0
> 1000000000.times {
> d = d + 1
> }
>
> puts d
>
>
> Needs: 8 minutes and 20 seconds.
>
>
> Does not look very good Is there a possibility to tune my
> ruby-program,
> to be as fast as mono?
>
> Greetings
>
> Mike
>




 
Reply With Quote
 
Alexander Staubo
Guest
Posts: n/a
 
      02-06-2005
Kent Sibilev wrote:
> Consider this 'tuning':

[snip]
>
> class Test
> def run
> d=0
> 1000000000.downto(1) {
> d = d + 1
> }

[snip]
> double d = 0;
>
> for (int i = 0; i < 1000000000; i++) {
> d = d + i;
> }


To be fair, the type of the variable in the Mono example is "double",
but the Ruby example uses an int. A more appropriate Ruby version:

def run
d = 0.0
1000000000.downto(1) {
d = d + 1.0
}
return d
end

Alexander.


 
Reply With Quote
 
Kent Sibilev
Guest
Posts: n/a
 
      02-06-2005
That's true, but this example is meaningless anyway. The point is that
if you find more than a couple of places in your application where Ruby
does not provide an acceptable performance, you probably should use
another language for the task. I just wanted to demonstrate that with
the help of RubyInline or even Ruby2C you can translate these
bottleneck spots without much of the hassle.

Cheers,
Kent.

On Feb 6, 2005, at 1:14 AM, Alexander Staubo wrote:

> Kent Sibilev wrote:
>> Consider this 'tuning':

> [snip]
>> class Test
>> def run
>> d=0
>> 1000000000.downto(1) {
>> d = d + 1
>> }

> [snip]
>> double d = 0;
>> for (int i = 0; i < 1000000000; i++) {
>> d = d + i;
>> }

>
> To be fair, the type of the variable in the Mono example is "double",
> but the Ruby example uses an int. A more appropriate Ruby version:
>
> def run
> d = 0.0
> 1000000000.downto(1) {
> d = d + 1.0
> }
> return d
> end
>
> Alexander.
>




 
Reply With Quote
 
Zach Dennis
Guest
Posts: n/a
 
      02-06-2005
Kent,

this was pretty hella cool. I'm going to check out Inline::C now that I
see an easy example and your benchmark. =) Gratzi,

Zach


 
Reply With Quote
 
George Moschovitis
Guest
Posts: n/a
 
      02-06-2005
WOW,

I am impressed!

George

 
Reply With Quote
 
Sam Roberts
Guest
Posts: n/a
 
      02-06-2005
Quoteing , on Sun, Feb 06, 2005 at 12:05:12PM +0900:
> Hi,
>
> because of my interest in mono and ruby I have done a small benchmark.


Ruby is an interpreted language. Its strengths are speed of development
and flexibility of implementation. C# is a compiled language.

Java and C# exist in a kind of half-world - more flexible than C++ in
some ways (GC, at least), but not as fast as the lower-level languages.

If you want to do fast math, you should be working in C or C++. If you
want both speed and a beautiful OO language, you write the math in C,
and bind it into ruby.

People doing cryptography in .Net do the same -> all the crypto code is
C/assembler, and it is bound in.

Cheers,
Sam



 
Reply With Quote
 
Alexander Kellett
Guest
Posts: n/a
 
      02-06-2005
On Feb 6, 2005, at 10:07 PM, Caio Tiago Oliveira wrote:
> I've made this benchmarking in some more languages (using d=d+1):
>
> In my Pentium 4 2Ghz 256Mb SDRAM 7200rpm:
>
> Java: 00m14,45s (d as long)
> Java: 00m12,70s (d as double)
> C: 00m03,27s (d as long int)
> C: 00m15,90s (d as double)
> Squeak Smalltalk: 01m48,60s
> Ruby: 18m12,21s
> Python: 14m04,83s


with c+llvm it is instantaneous.
Alex



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mono, DataGrid and Data formatting expression camelweb@bigpond.com ASP .Net 1 05-30-2005 03:11 PM
Gtk/Gnome-Applications with Ruby or Mono Michael Gebhart Ruby 7 02-05-2005 11:54 AM
Ruby and Mono Hal Fulton Ruby 11 08-12-2004 10:01 PM
Ruby and .NET/Mono Hal Fulton Ruby 1 08-10-2004 07:27 AM
Mono Manat K. ASP .Net 3 08-11-2003 03:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57