Speed test: PHP vs Perl vs Python vs Go vs C

Recently I have needed to run a cron job every minute on a high load web server. As am currently most fluent in PHP I naturally wrote my script in PHP. However, as it’s running every minute, I do want it to be as fast as possible. Seeing as the script was very simple, I thought that the most time required to run is in loading the PHP interpreter. Maybe i could use a compiled language instead? What about Google’s new language Go?

NOTE: This was not an overall speed test. I am just trying to find out which approach would be fastest for running a cron job every minute. None of the programs tested run through a web server.

Anyway, for my test I wrote a “hello, world” command line app in each language and ran the program 1000 times. Without any further ado, here are the results, from fastest to slowest:

C: 5.37 seconds

Go: 6.23 seconds

Perl: 7.97 seconds

Python: 20.64 seconds

PHP: 41.31 seconds

There you go! PHP is a bit slow for this kind of thing! But I’m not going to write all my scripts in Go or C now (what a hassle!). So, for me…. perl wins!

Ahh, good old perl, my faithful friend! I will never forget you!

36 thoughts on “Speed test: PHP vs Perl vs Python vs Go vs C”

  1. Greg, I’m not running any of these tests through a web server at all – only from the command line.

    Interesting point about mod_perl though – it reminds of when I switched from Perl to PHP for web apps years ago. PHP was so much faster because the interpreter was build into apache (mod_php). Perl had a horrible delay with each request as the perl interpreter had to be loaded with each request. mod_perl was the answer – but when i tried to port my apps to it – disaster! Not thread safe!

    When I get time I’ll run these simple hello world tests again through apache.

  2. Yes, running a script as a cron job, does not run through the web server. These are generally used for update/cleanup methods that run outside of the web server. Interesting about mod_perl though, is that most people tend to use the Registry method. I’ve found creating your own handlers to be way more efficient and fast. Plus, it gives you far more power over the entire request that you can’t get with PHP. You can do everything from programmatic url rewriting to remapping requests to specific files. For example, I wrote a handler that would handle file uploading in a more correct fashion, as it allows me to cancel posting while in progress. Something PHP can’t do, you have to wait until the entire post is done before your script fires.

    You are right though, about the thread safe. Although, most Apache installations are still MP and not MPMT or MT. Most scripts people write for Registry are not MP safe.

  3. There must be some per-process overhead with your Python binary that doesn’t exist with the Perl one… This overhead, which happens only once per script execution, is hardly significant unless you run a script many times per second. Start-up time aside, Python is generally a bit faster than Perl.

  4. Phyton is not faster as Php, you are wrong. Maybe you have a bad compiled php version or some bad module.

  5. Sure, why don’t you run your own trials and post the results.

    And remember people, I’m talking about command line scripts here.

    Can’t people read anymore or something?

  6. @PHP Programmer:
    Actually PHP being 8 times slower than C is wildly inaccurate, probably because this benchmark is heavily weighted toward I/O, which will blur the speed differences between languages. As far as the language goes, you can expect that C would be 50 – 80 times faster than an interpreter like PHP or Python.

  7. Hi Wilsa,
    This was a real world test. How about backing up your ‘theories’ with some practical tests of your own before spouting out “wildy inaccurate” crap.

  8. The biggest advantage of PHP over Perl is that PHP was designed for scripting for the web, while Perl was designed to do a lot more. Because of this, Perl can get very complicated. The flexibility / complexity of Perl can make it difficult for developers of varying skill levels to collaborate. PHP has a less-confusing and stricter format without losing flexibility. PHP is also easier to integrate into existing HTML than Perl. In large part, PHP has all the ‘good’ functionality of Perl – constructs, syntax, et cetera – without making it as complicated as Perl can be. Yet PHP’s command-line interpreter (CLI) is powerful enough to perform high-level tasks much in the same way Perl has been traditionally employed. Perl is a very tried and true language, and has stood its ground since the 1980’s, but PHP has matured and evolved quickly, and continues to make fantastic progress.

  9. Thanks anon… yeah y’know you’re obviously right – you are the only one true programmer out there. All honour to you.. king of the programmers. Now back into your little hole..tosser.

  10. It seems that the new guidelines in Web 2.0 call for separating behavior from structure from style. With the re-emergence of Javascript(and jQuery), newer websites are not incorporating PHP into their HTML.

    Perl seems to be a much cleaner, stricter, and more powerful language than PHP. And I have worked with both.

  11. You can separate structure and style in PHP too – if you want to. The thing about PHP is that you are free to do whatever you want. You can be OO – you can be procedural. You are FREE. If you dont like freedom then yes, choose a prison language like Python or Java or Perl or whatever ;)

    Languages don’t write bad code – people write bad code!

  12. I do not mean this as a personal jab, but this is not a valid speed test. Many people are not aware that printing to the screen is an Operating System call, not a feature of the language. The reason for variation may have to with constraints; such as a web-server processing the data printed out by PHP.

    A better speed test is held in categories such as: matrix multiplication, sorting, and mathematical computations like finding prime numbers.

    Your speed test basically comes down to this:

    do 1000 times
    call OS write function with “Hello World” to standard out file
    process file with OS/Server

  13. Scott, did you read my post? Or just skim read it? “Recently I have needed to run a cron job every minute on a high load web server” the speed test here includes starting up the interpreter. Because I am running a cron job every minute. This is not an overall performance test. This was a test to see which language was the fastest for this job. This was not an overall speed test. I was just trying to find out which approach would be fastest for running a cron job every minute. None of the programs tested run through a web server. They were all on the command line. This is not a web server speed test. This is not a general language speed test. This was a practical test to see which program would execute fastest for the purposes of a one minute cron job.

  14. I did end up skimming past some of those details. So, I would imagine the start-up and implementation of the interpreter would matter.
    I do agree with your idea that perl would be the best for the job as you described it.

  15. No worries, Scott – who has time to read everything these days anyway? :)
    I’ve put the disclaimer in bold now!

    I’m going to include Ruby in the test too now…

  16. To what Scott said, I believe is part of a programming language to make OS calls, so, it is ok to me the performance test that was done to compare cronjobs execution time. My opinion, you use the language that fits the most to each specific task. For a web applicacion I would use PHP because the for web application PHP interpreter is compiled with Apache, which will help with the performance, for back end scripts and batch execution I would use perl… it all depends on what you like/know, what you need to accomplish and the environment(cli, gui, etc)

  17. Has anyone tried Speedy::CGI for perl ?


    This is an amazing module for Web or command line perl script. It creates a persistent image of the post-interpreter (compiled?) code in the memory, i.e the interpreter runs only once. You can specify the timeout when this image can be destroyed if not accessed.

    We have seen result improvements on repetitive run upto 1:10 on some heavy I/O + memory hogging scripts.

    Most of already written scripts can run under Speedy, however some attention must be given to permanent and volatile variables data by using ‘my’ and ‘our’ for variable declaration to avoid ambiguous variable state on next invoking of the script

  18. time php -r “foreach(range(1000) as $i) { echo ‘hello world’; }”
    real 0m0.028s
    user 0m0.012s
    sys 0m0.012s

    time python -c “for i in range(1000): print ‘hello'”
    real 0m0.031s
    user 0m0.016s
    sys 0m0.008s

    time for i in {1..1000}; do python -c “print ‘hello'”; done
    real 0m21.730s
    user 0m14.997s
    sys 0m4.520s

    time for i in {1..1000}; do php -r “echo ‘hello'”; done
    real 0m25.197s
    user 0m13.913s
    sys 0m9.309s

  19. Okay, I don’t know about this so-called “speed test”.

    I can get a simple PHP script to spit out 1000 “Hello Worlds” in less than one second. Same for Python. 41 seconds to do anything in PHP is a martal sin, and for this reason I question the skills of the programmer as well as the validity of the test.

    Of course, there is no way to know exactly HOW you came up with these numbers, since there isn’t one iota of code on here. SO I guess I’ll just have to take your word for it.

    In the meantime, I will be looking forward to your next benchmark with fibonacci numbers, as we all know how many people are looking for a scalable fibonacci-numbers-crunching app.

  20. Lets do science. I will start. Here are my tests and my results. Please try and share.

    I had wanted to start using node for devops but it poor showing printing 1000 hello worlds is making me think twice. NOT! :D

    for i in {1..1000}; do php -r ‘echo “hello world “;’; done;
    46 seconds

    for i in {1..1000}; do php -r ‘print (“hello world “);’; done;
    42 seconds

    for i in {1..1000}; do perl -e ‘print “hello world “;’; done;
    28 seconds

    for i in {1..1000}; do python -c ‘print(“Hello world “)’; done;
    27 seconds

    for i in {1..1000}; do echo “Hello World “; done;
    3 seconds

    for i in {1..1000}; do node -e “console.log(‘Hello, world’);”; done;
    52 seconds

    for i in {1..1000}; do ruby -e ‘puts “Hello world “‘; done;
    5 seconds

    for i in {1..1000}; do echo ‘puts “Hello World “‘ | tclsh; done;
    16 seconds

  21. Thanks for the post Joshua, you’re right – node may not be the most appropriate solution for running command line cron job tasks every minute

  22. While I understand the premise of this experiment, in reality, and even with authors use-case, it is a very poor test.

    If your running php once a minute, you do not need to calculate how long it takes to invoke php 1000 times.

    Running php 1000 times on my machine gets me to about 7~ seconds. Running it once is 0.03~ seconds.

    Your speed test doesn’t measure language speed, rather how fast the interpreter loads.

    Everyone would greatly benefit if your test shows your cron code running in different languages and calculating how long THAT takes to execute then showing the results.

    This test is the equivalent of getting a couple of fast cars then judging performance on how quick it takes for the car to start.

  23. Ozzy Ozzy Ozzy… oi oi oi

    “This test is the equivalent of getting a couple of fast cars then judging performance on how quick it takes for the car to start.”

    YES – great analogy – perfect – that’s exactly what I’m doing here!

    Sometimes people dont care about how fast the car is, but how fast they can get in a FLEE THE CRIME SCENE!

  24. I am disagreeing with your results as you have performed practically but i have read a blog on this topic and according to that php is faster then python.

  25. Oh, if you read it on a blog post then it must be true! Doesn’t that make my blog post true too? Oh no, two contradictory blog posts are both true! The universe collapses!

  26. Using the shell’s range is obviously going to be slower than making the tested language do the loop:

    $ time php -r “foreach(range(1000) as $i) { echo ‘hello world’; }”
    real 0m0.094s
    user 0m0.076s
    sys 0m0.018s

    $ time python -c “for i in range(1000): print ‘hello world'” > /dev/null
    real 0m0.041s
    user 0m0.035s
    sys 0m0.006s

    $ time perl -e ‘for (1..1000) { print “$_ hello world\n”; }’ > /dev/null
    real 0m0.010s
    user 0m0.006s
    sys 0m0.003s

    package main
    import “fmt”
    func main() {
    for i := 0; i /dev/null
    real 0m0.005s
    user 0m0.003s
    sys 0m0.002s

    main() {
    int i;
    for (i = 0; i /dev/null
    real 0m0.003s
    user 0m0.000s
    sys 0m0.003s

    Using bash for looping, I get:

    $ time for i in {1..1000}; do php -r “echo ‘hello world’;” > /dev/null; done
    real 1m39.679s
    user 1m20.376s
    sys 0m18.370s

    $ time for i in {1..1000}; do python -c “print ‘hello world'” > /dev/null ; done
    real 0m37.151s
    user 0m30.297s
    sys 0m6.776s

    $ time for i in {1..1000}; do perl -e ‘print “$_ hello world\n”‘ > /dev/null ; done
    real 0m10.943s
    user 0m7.426s
    sys 0m3.087s

    package main
    import “fmt”
    func main() {
    fmt.Println(“hello world”)
    $ time for i in {1..1000}; do main > /dev/null ; done
    real 0m2.570s
    user 0m1.100s
    sys 0m1.455s

    main() {
    printf(“hello, world\n”);
    $ time for i in {1..1000}; do a.out > /dev/null ; done
    real 0m1.854s
    user 0m0.632s
    sys 0m1.066s

  27. Also, pure Bash falls between Python and Perl:

    $ time for i in {1..1000}; do echo “hello world” > /dev/null ; done
    real 0m0.034s
    user 0m0.027s
    sys 0m0.006s

Leave a Reply

Your email address will not be published. Required fields are marked *