Comparing Ruby vs Python vs PHP
By AkH, 3 years ago, modified July 8, 2007
Jedi made some testing to show up how PHP is so bad at object oriented programming vs Ruby, so I tried the same test in Python:
class TestPy(object):
def __init__(self, string):
self.string = string
self.counter = 0
def testfunc(self):
self.string += 'abc '
'-'.join(self.testfunc2())
def testfunc2(self):
array = self.string.split()
str = ''
for word in array:
if not str or self.counter < 0:
str += word
self.counter += 1
return array
str = "initial string"
a = TestPy(str)
for x in range (0,4999):
a.testfunc()
The Ruby code from Jedi: 0m17.576s with ruby 1.8.2
This Python code: 0m8.386s with python 2.5 and 0m8.958s with python 2.4.4, on an Imac 24".
The goal here is not to have the fastest one, as many optimizations could be made on this really dumb code, the test just prove the good implementation of boths Python and Ruby but not PHP
Who wants to do the same in Java ?


Comments
Hi,
I tried the same thing in Java. Not completly sure that the algo is equivalent but it took 5,799ms on a Core 2 Duo 2,16Ghz / 3 GB RAM with Java SE 6 on Mac OS X Tiger (the preview version).
Here the code source (any comments are welcome) :
package net.cowmeuh.kdl;
public class LightningFastIteration {
private String string;
private int counter;
public LightningFastIteration(String initialString) {
string = initialString;
counter = 0;
}
public String startIteration() {
string += "abc ";
StringBuilder buffer = new StringBuilder(50);
String[] array = iterate();
for (int i = 0; i < array.length; i++) {
String aString = array[i];
buffer.append(aString);
buffer.append("-");
}
return buffer.toString();
}
public String[] iterate() {
String[] array = string.split(" ");
String buffer = "";
for (int i = 0; i < array.length; i++) {
String string = array[i];
if (counter < 0) {
buffer += string;
}
counter++;
}
return array;
}
/**
* @param args
*/
public static void main(String[] args) {
long start = System.currentTimeMillis();
LightningFastIteration i = new LightningFastIteration("initial string");
for (int j = 0; j < 4999; j++) {
String output = i.startIteration();
if (j % 1000 == 0)
System.out.println(output);
}
System.out.println("Total time: "
+ new Long(System.currentTimeMillis() - start) + " ms.");
}
}
You are measuring something different (cheater :) ): without the jvm startup so we should measure all the same things, but your test is interesting too.
But first make your code do the same as jedi's code :)
Hello Akh,
if not str or self.counter < 0:
should probably be:
if not (not word or self.counter < 0):
and "counter" should be a class-local variable.
Anyway I doubt it will change a lot the results.
When it comes to object-oriented programming, it would be also interesting to compare how long it takes for the coder to complete the task.
Since PHP's base library is nothing but a pile of messy functions, if you want a clean, class-based interface, you need to reinvent the wheel. It's quite pathetic to see that every project reimplements its own set of classes in order to access databases.
The more I code with PHP, the more I realize that most of the time is spent writing _how_ to do things (even obvious ones) than _what_ things to do. And the more I try to do clean PHP, the more I realize that ugly hacks are inevitable with that language. It"s frightening. This is why I love Ruby, it gives me the feeling of concentrating on actually doing things, rather than reinventing the wheel over and over again.
Take that:
http://download.pureftpd.org/misc/s...
It runs its own web server, waits for requests such as http://127.0.0.1:2000/rss?u=borat and it returns a complete RSS feed of that user's skyrock blog. It iterates over all pages in the right order, it includes pictures and descriptions, etc. You can use it at http://rss.valid.orbus.fr/rss/?u=.....
It manually fetches web pages in order to parse them, as it was something that I needed way before Skyblog had its own RSS feeds. It was written about 15 minutes, it's short, modular and readable (well, if you know a bit about Ruby).
Now here's a challenge. Try to do the same app in your favorite language. PHP? Okay, do it with PHP if you want to.
It should include its own webserver. It should return a full RSS feed with the 15 (configurable) latest articles (of course it should detect if the web pages are listing articles in chronological order or not). The correct titles and descriptions have to be there. Pictures should be linked in articles.
How long did you spend writing this? How long is your code (make it clear, readable, modular, not intentionnally overcondensed)? How much bloat did you write (things like $this->, self:: and other stuff the language forces you to write just to avoid syntax errors)?
I'm pretty sure that with some work, a PHP version would easily beat Java, Ruby and Python when it comes to speed (CPU time). But I'm pretty sure that the PHP version would require way more human-time. Less time to complete a project means free time to polish it. Ie. time to focus on _real_ optimizations (caching, database work, etc). Something that we all want to do but that we usually can't do because of crazy deadlines. Having free time to work on a project is the way to optimize it and to make it reliable instead of relying on last-minute kludges.
Who cares if language X is faster than language Y on some microbenchmarks? Frameworks like Django or Rails are heavy and add extra overhead? Sure. But thanks to those frameworks, you don't waste your time reinventing the wheel for every new project. Caching is simple. Making a scalable application is simple. Maintaining the application is simple. The developper is not bored. The boss enjoys having a polished application, and on time. Unfortunately, it implies that other people from your team agree. That's the trickiest point.
Hi Jedi
Thanks for your comments.
For ppl wants to look at jedi's code the right link is http://download.pureftpd.org/misc/r...
You're right the fastest is not the greatest, a simple language with bunch of good APIs will beat everything else when it comes to measure human time and fun.
But don't be so rude at Java they made amazing changes on the JVM, and there is now some good frameworks, you could be surprise how it performs.
I also think that a good language with structured framework give a path in the right direction to the developper avoiding a lot of errors, and hidind a lot of optimizations.
Developpers are not always a John Carmack.
In the other hand learning curve for a new framework could be really time consuming, so depending on the size of your project, choose the best.
On the company choices, we had the same issue, a guy made a nice rails apps for us then he leaves after 4 months, with no rails guru we were in trouble.
Enterprise will never go as fast as the community, that's a tricky issue cause sometimes you feel sad about your old ugly apps in this so ugly language, but we have to remember at the time of this choice, it has done the job ...
Anyway PHP is a deep shit and should be avoided everywhere :)
tried the same thing in Java. Not completly sure that the algo is equivalent but it took
I kept encountering the same problem. The issue turns out was a badly spelled Path variable.
Make sure that both the path to jri.dll and the path to R.dll, are included in the PATH variable and that there is no space in between the two values in the PATH variable. As soon as I fixed that - the program ran fine
A drunk man who was running naked into highway traffic and subsequently caused three accidents has been arrested.
Two people stopped their cars and tried to help the 26 year old, Ardonas Gilbert of Chester P.A., who had been running naked along the southbound lanes of Interstate 95 this past Monday night. However Ardonas refused their help and allegedly cursed at them and tried to punch them says a Delaware State Police officer. <a href="http://www.fashionerpop.com/">discount mens jacket</a>
After hitting the people trying to help him, Gilbert then ran out into traffic and caused three separate accidents as motorists tried to avoid hitting the man. Luckily no one was seriously injured. <a href="http://www.fashionerpop.com/">mens hoodies</a>
Gilbert was charged with two counts of assault and a single count of being drunk on a highway.