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

标签: perl python php benchmarking ruby spreadsheet
4781人阅读 评论(0) 收藏 举报

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


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.

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.


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.


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.


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.


【最优化方法】穷举法 vs. 爬山法 vs. 模拟退火算法 vs. 遗传算法 vs. 蚁群算法

优化算法入门系列文章目录(更新中):   1. 模拟退火算法   2. 遗传算法   一. 爬山算法 ( Hill Climbing )          介绍模拟退火前...
  • kuvinxu
  • kuvinxu
  • 2014年06月29日 08:46
  • 2391


以前没怎么弄过堆溢出问题,直到最近看见Google研究团队发布了关于dnsmasq的一系列问题 (需要翻墙)其中有两个CVE涉及到了Heap Overflow。其中的基础知识不再赘述,可以直接参考以下...
  • iextract
  • iextract
  • 2017年10月12日 18:12
  • 426


内存、指针相关 当程序运行出现退出错误为以下任意:(xxxxxxxxxx为地址) free():invalid pointer: xxxxxxxxx     malloc(): mem...
  • prliebtsy
  • prliebtsy
  • 2018年01月26日 21:39
  • 91


正如世界上的语言有多种多样,编程语言同样如此,有些更受欢迎,有些简单易用。随着编 程语言的种类越来越多,它们之间的竞争也日益激烈。而且往往简单的、运行速度快的编程语言并非就是最好的选择,因为不同的程序...
  • qingtingchen1987
  • qingtingchen1987
  • 2012年08月18日 20:46
  • 1129

Ruby on Rails vs. PHP vs. Python

开发者在开发web应用时,往往会对平台的选择感到困惑,而web专家通常会建议:要考虑几个因素,这些因素包括周转时间、质量、跨浏览器兼容性、与其他框架的整合、数据安全性、易于访问性等。 在 考虑了这些...
  • hopepower
  • hopepower
  • 2012年11月23日 22:05
  • 215

运行caffe时报错:Check failed: status == CUDNN_STATUS_SUCCESS

cudnn_conv_layer.cpp:53] Check failed: status == CUDNN_STATUS_SUCCESS (6 vs. 0)  CUDNN_STATUS_ARCH_M...
  • wuzuyu365
  • wuzuyu365
  • 2016年04月15日 17:07
  • 7704

Check failed:error == cudaSuccess(30 vs. 0) unkown error

今天被这个问题折腾惨了。昨天Caffe使用还一切正常,今天就突然出现了这个问题,如下图所示: 我尝试重新安装CUDA,重新编译安装Caffe,都没有成功解决。后来灵机闪现,想到是不是显卡驱动的问题...
  • Sunshine_in_Moon
  • Sunshine_in_Moon
  • 2016年12月06日 20:07
  • 5950

faster-rcnn训练时出现error == cudaSuccess (30 vs. 0)

./experiments/scripts/ 0 ZF pascal_voc   使用这条命令训练,出现下面这个错误 Check faile...
  • qq_26569761
  • qq_26569761
  • 2016年06月22日 21:39
  • 5538


caffe出现这个问题时,总让人摸不着头脑,不知从哪儿开始排查问题。     网上搜索资料,发现有人说是label做得不对,比如只有1没有0。但是这个是问题吗?就不能只有一个标签吗?     不过...
  • thesby
  • thesby
  • 2016年06月22日 14:41
  • 8617

Dogs vs. Cats Redux: Kernels Edition

训练样本数量超过内存,采用python生成器 基于keras + tensorflow+Resnet-34 直接上代码 #coding=utf-8 import numpy as np impo...
  • wmy199216
  • wmy199216
  • 2017年05月04日 21:13
  • 513
    访问量: 62万+
    积分: 8749
    排名: 2697