Ruby faster than Python and Perl!

When Pavel microbenchmarked every language he came across it turned out as expected, Ruby was the slowest language in all three micro tests. It was really embarrassing.

I was curious if this was about to change in future versions of Ruby so I checked out the current Ruby 1.9 version from their svn repository

svn co http://svn.ruby-lang.org/repos/ruby/trunk ruby

After compiling I ran Pavels microbenchmarks with the current stable Ruby 1.8.6, with the SVN version 1.9, Python 2.4 and Perl 5.8.8. Here are the results. I ran every Benchmark three times:

Ruby 1.8.6


$ ruby ~/Desktop/simple-bench.rb
Pavel's trivial ruby benchmark V.0.1
1000000 x {i+=1} took 0.243 sec. (4.114 mio. per sec)
2000000 x {i = my_add(i, 1)} took 0.940 sec. (2.128 mio. per sec)
50000 x {i = fullMatMult(a, b)} took 2.608 sec. (19172.294 per sec)

$ ruby ~/Desktop/simple-bench.rb
Pavel's trivial ruby benchmark V.0.1
1000000 x {i+=1} took 0.240 sec. (4.175 mio. per sec)
2000000 x {i = my_add(i, 1)} took 0.941 sec. (2.126 mio. per sec)
50000 x {i = fullMatMult(a, b)} took 2.611 sec. (19149.538 per sec)

$ ruby ~/Desktop/simple-bench.rb
Pavel's trivial ruby benchmark V.0.1
1000000 x {i+=1} took 0.242 sec. (4.135 mio. per sec)
2000000 x {i = my_add(i, 1)} took 0.943 sec. (2.122 mio. per sec)
50000 x {i = fullMatMult(a, b)} took 2.626 sec. (19038.328 per sec)

Ruby 1.9


$ /usr/local/bin/ruby ~/Desktop/simple-bench.rb
Pavel's trivial ruby benchmark V.0.1
1000000 x {i+=1} took 0.142 sec. (7.040 mio. per sec)
2000000 x {i = my_add(i, 1)} took 0.409 sec. (4.893 mio. per sec)
50000 x {i = fullMatMult(a, b)} took 0.660 sec. (75701.488 per sec)

$ /usr/local/bin/ruby ~/Desktop/simple-bench.rb
Pavel's trivial ruby benchmark V.0.1
1000000 x {i+=1} took 0.142 sec. (7.023 mio. per sec)
2000000 x {i = my_add(i, 1)} took 0.410 sec. (4.882 mio. per sec)
50000 x {i = fullMatMult(a, b)} took 0.658 sec. (75979.874 per sec)

$ /usr/local/bin/ruby ~/Desktop/simple-bench.rb
Pavel's trivial ruby benchmark V.0.1
1000000 x {i+=1} took 0.143 sec. (7.002 mio. per sec)
2000000 x {i = my_add(i, 1)} took 0.409 sec. (4.889 mio. per sec)
50000 x {i = fullMatMult(a, b)} took 0.660 sec. (75772.730 per sec)

Python 2.4


$ python2.4 ~/Desktop/simple-bench.py
Pavel's trivial python benchmark V.0.1
10000000 x {i = i + 1} took 2.54 sec. ( 3.93700787402 mio. per sec), i= 10000000
5000000 x {i = my_add(i, 2)} took 2.46 sec. ( 2.0325203252 mio. per sec)
100000 x {i = fullMatMult(a, b)} took 1.75 sec. ( 57142.8571429 per sec)

$ python2.4 ~/Desktop/simple-bench.py
Pavel's trivial python benchmark V.0.1
10000000 x {i = i + 1} took 2.54 sec. ( 3.93700787402 mio. per sec), i= 10000000
5000000 x {i = my_add(i, 2)} took 2.46 sec. ( 2.0325203252 mio. per sec)
100000 x {i = fullMatMult(a, b)} took 1.74 sec. ( 57471.2643678 per sec)

$ python2.4 ~/Desktop/simple-bench.py
Pavel's trivial python benchmark V.0.1
10000000 x {i = i + 1} took 2.54 sec. ( 3.93700787402 mio. per sec), i= 10000000
5000000 x {i = my_add(i, 2)} took 2.47 sec. ( 2.02429149798 mio. per sec)
100000 x {i = fullMatMult(a, b)} took 1.72 sec. ( 58139.5348837 per sec)

Python 2.5


$ python2.5 ~/Desktop/simple-bench.py
Pavel's trivial python benchmark V.0.1
10000000 x {i = i + 1} took 2.5 sec. ( 4.0 mio. per sec), i= 10000000
5000000 x {i = my_add(i, 2)} took 2.53 sec. ( 1.97628458498 mio. per sec)
100000 x {i = fullMatMult(a, b)} took 1.78 sec. ( 56179.7752809 per sec)

$ python2.5 ~/Desktop/simple-bench.py
Pavel's trivial python benchmark V.0.1
10000000 x {i = i + 1} took 2.52 sec. ( 3.96825396825 mio. per sec), i= 10000000
5000000 x {i = my_add(i, 2)} took 2.52 sec. ( 1.98412698413 mio. per sec)
100000 x {i = fullMatMult(a, b)} took 1.78 sec. ( 56179.7752809 per sec)

$ python2.5 ~/Desktop/simple-bench.py
Pavel's trivial python benchmark V.0.1
10000000 x {i = i + 1} took 2.5 sec. ( 4.0 mio. per sec), i= 10000000
5000000 x {i = my_add(i, 2)} took 2.53 sec. ( 1.97628458498 mio. per sec)
100000 x {i = fullMatMult(a, b)} took 1.78 sec. ( 56179.7752809 per sec)

Perl 5.8.8


$ perl ~/Desktop/simple-bench.pl
Pavel's trivial perl benchmark V.0.1
20000000 x {i = i + 1} took 2.34329295158386sec. (8.534997720401 mio. per sec), i=20000000
5000000 x {i = my_add(i, 2)} took 3.05162501335144sec. (1.63847129910263 mio. per sec)
100000 x {i = fullMatMult(a, b)} took 3.62118482589722sec. (27615.2709148788 per sec)

$ perl ~/Desktop/simple-bench.pl
Pavel's trivial perl benchmark V.0.1
20000000 x {i = i + 1} took 2.35069394111633sec. (8.50812589856002 mio. per sec), i=20000000
5000000 x {i = my_add(i, 2)} took 3.05917191505432sec. (1.63442923079765 mio. per sec)
100000 x {i = fullMatMult(a, b)} took 3.61791110038757sec. (27640.2590404411 per sec)

$ perl ~/Desktop/simple-bench.pl
Pavel's trivial perl benchmark V.0.1
20000000 x {i = i + 1} took 2.3465690612793sec. (8.52308177501345 mio. per sec), i=20000000
5000000 x {i = my_add(i, 2)} took 3.05434393882751sec. (1.63701275957788 mio. per sec)
100000 x {i = fullMatMult(a, b)} took 3.62034606933594sec. (27621.6687810573 per sec)e178085100

All of Pavels microbenchmarks can be found here: http://acker3.ath.cx/wordpress/archives/11

I ran these tests on a MacBook Pro 2,33 GHz Intel Core 2 Duo with 2GB of RAM and some Apps running.

To be continued. And yes I know microbenchmarks are not saying anything meaningful.

[UPDATE 1]

So for completeness sake I also ran the benchmark with Python 2.5 but that didn’t really improve anything in this particular microbenchmark. The conclusion of this little exercise is that the new ruby interpreter really seems to be a lot faster than the old one. Now even ahead of some other scripting langunages in terms of speed. That is so good news!

Symbols in Ruby – Short Explanation?

When I started learning Ruby and Ruby on Rails I was introduced to the concept of symbols. You are using a lot of symbols in Rails and it feels somewhat natural after a while but when you try to explain newbies what these symbols are actually good for, it is really hard to describe. It is especially hard because you could also use strings instead. At that point you don’t actually even know the difference between a string and a symbol is or what symbols are even good for.

I just played with the interactive ruby shell => irb and found out a short way of describing what symbols are good for:


$ irb
irb(main):001:0> "foo".object_id
=> 36730
irb(main):002:0> "foo".object_id
=> 23400
irb(main):003:0> "foo".object_id
=> 10710
irb(main):004:0> :foo.object_id
=> 145858
irb(main):005:0> :foo.object_id
=> 145858
irb(main):006:0> :foo.object_id
=> 145858
irb(main):007:0>

If you would use strings to name an object in some kind of association, you would actually create new objects every time, which is quite unnecessary. Symbols are just an efficient way to point to objects or pass arguments to a method or association without creating new string objects all over the place. It is like saying »The variable or object call :foo«. Hmm got it?

I know – it is probably a no-brainer, but I had difficulties explaining it properly.

Aus Grün mach Rot

Vor ein paar Jahren, als man Bündnis ’90 / Die Grünen noch ernstnehmen konnte war die Abschaltung von AKW’s noch ein Nischenwunsch. Heute schreibt sich die SPD dieses Ziel auf die Flagge. Wenn man diese Politiker und deren Versprechen doch nur ernstnehmen könnte. Passend zum Thema auch ein schönes Fundstück bei Fefe (omfg! ich verlinke auf Fefe’s Blog)

SPD will AKWs abschalten

Ruby on Rails debugging with breakpoints

I’m currently working on the software (called CCCash) which will be used to sell the tickets at the Chaos Comunication Camp 2007. During the development process we had a strange bug which we couldn’t really figure out. Pandur, who is also working on the software and who is more experienced than me in terms of RoR used breakpoints to figure this one out. I haven’t read the Pragmatic book this far so I didn’t know that this was even possible. It worked just as you would expect it. See the Ruby on Rails wiki for more informations. The more I dig into Ruby on Rails, the more I like it.

SelfHTML auf dem Chaos Communication Camp

Eben erst in meinem Feedreader entdeckt: SelfHTML WebDevVilage auf dem Chaos Comunication Camp 2007

Ich bin auf dem Camp und werde wohl mal vorbeischauen – vielleicht interessiert es ja auch den einen oder anderen meiner werten Leser. Lohnt sich sicher!