秋水之城

酒罢歌梁祝,情多付婵娟。

Benchmark: PHP vs. Python vs. Perl vs. Ruby

Benchmark: PHP vs. Python vs. Perl vs. Ruby

转自:http://xodian.net/serendipity/index.php?/archives/27-Benchmark-PHP-vs.-Python-vs.-Perl-vs.-Ruby.html


Today, I was looking at a couple of PHP benchmarks (the one here for example), and they compared OOP benchmarks to Procedural benchmarks for the most part. I decided to write my own set of benchmarks using four of the most well-known web development languages that were ready-at-hand: PHP, Python, Perl, and Ruby. My benchmark also tests OOP vs. Procedural on a few terms: Execution speed and Raw lines of code written. I compare a visual estimate of memory usage between all four languages as well.

Now, one cannot draw very many conclusions from my benchmarks here, because the programs used don't really do anything useful, nor do they simulate "real-world" conditions, or practical use-cases. Remember, we learn multiple programming languages, not because of fads, but because it expands our own knowledge, and because some languages are better than others for some things; and it's helpful to be able to choose the right tool for the job.

Keep in mind the golden rule of benchmarking: All benchmarks are flawed benchmarks.
Conditions

CPU: 64-bit AMD Sempron 3000+ @ 1 GHz
OS: Gentoo Linux (x86-64) w/kernel: 2.6.22-gentoo-r6
RAM: 1 GB total

Scripting Language Versions:

PHP: 5.2.5-pl0-gentoo w/suhosin
Perl: 5.8.8 
Python: 2.5.1
Ruby: 1.8.6 (2007-12-03 patchlevel 113)

All tests were run consecutively, and thus had approximately the same amount of memory and CPU time available to them.

Method

A bash script was used to automate testing and record results. A perl script was used to average the run times and give a result.

Test Programs

Two test programs were measured:

Hello - Prints "Hello World" to stdout.
Inc - Increments a variable, starting at 0.

Each program does one-million iterations, and is run ten consecutive times. It's also worthwhile to note that for these type of benchmarks use of OOP or a function would be impractical.

The test programs, shell script, perl script, and an Open Office spreadsheet can be downloaded here.

Results



Hello benchmark: (Times are in seconds)

           OO      Func.  Neither
PHP:      4.1248  3.7656  0.9309 
Ruby:	  2.8940  2.7772  2.1919
Python:	  1.7893  1.7498  1.3138
Perl:	  1.3188  1.1824  0.6986





Inc benchmark: (Times are in seconds)

           OO      Func.  Neither
PHP:      3.7079  4.3501  0.6164 
Ruby:	  1.5032  1.3473  0.8810
Python:	  1.0387  0.8543  0.5792
Perl:	  0.4870  0.9355  0.4877




Total Lines of code (including comments, etc.): 

          OO      Func.  Neither
PHP:      32      24      14 
Ruby:	  41      26      18
Python:	  33      23      16
Perl:	  61      28      19




The memory usage is a percentage, obtained by watching top, and averaging the results across runs of the programs.

Conclusions

As I stated earlier, these benchmarks are not the kind you'd use OOP for (or even a function for code reuse.) However, these tests reveal that the slowest-executing programming method is: poorly-used OOP, which unfortunately is common-place nowadays. Excessive function calls also tend to add a good amount of overhead, and make for a good example of poor procedural programming. Performance really depends more on how the code is written as opposed to the paradigm in which it is written.

I'll admit, I despise 'traditional' OOP. It's messy, over-complicated, and completely unnecessary (I could rant all day.) I am, and always will be, a purely procedural programmer, and a pedantic bastard. One can have 'object-oriented' code, outside the scope of traditional OO, and traditional OO languages; in a more procedural fashion. That said, I have not biased these tests in any way.

The Neither column is the one that should be used to compare the four languages really, and going based upon that, one can conclude that writing these tests in any of the four doesn't require much more or less code than any of the others. From an execution-time perspective, Perl is the fastest overall. Perl also used the least memory overall.

Because of the number of iterations done within each test program (1 million), for simple scripts you're not going to see much of a difference in execution time between the four. However, for intensive tasks, you may see a very large difference, with Perl most-likely being faster than the other three; though as I previously stated, it all depends on how you write the code.

Eventually, I will write a few good benchmarks to show the substantial differences between OOP and procedural programming.

阅读更多
个人分类: Web开发
想对作者说点什么? 我来说一句

软件测试方法的平衡之美

2011年08月04日 1.08MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭