Using FizzBuzz to Compare Speed in Perl vs. PHP
I know a lot of people dislike Perl, but I programmed a lot of large applications in it for many years. I agree that it can be difficult for programmers new to Perl to pick up on some of its nuances, but I disagree that it’s a complete mess/line noise/write-only. Good clean code can be written in any language, just like horrible disgusting code can be written in any language.
A while back I saw a language speed shootout, whereby the same version of a script was written in many different languages and compared against each other to see how quickly they ran. Surprisingly, Perl has been so well optimized, that its performance was right on par with the C and C++ versions of the test. PHP, Ruby, Python, Java, etc. were all comparatively much slower. You could see the iterations of each getting much longer as the memory use increased. Yet Perl (and the C’s) stayed pretty consistent throughout.
Not being satisfied with someone else’s benchmarks, I ran the programs on a local machine. While the times were different, the results were nearly identical in terms of performance trends.
Last year when I was hiring a developer for my team, I used the FizzBuzz application as a coding exercise for the candidates to weed out the people that claimed they could program but couldn’t. Of course, this lead to a throw-down amongst the current crew as to how each of us would write it if we had too.
I eventually came up with my minimalist version, then realized that with just a few tweaks, I could make the code run under either Perl of PHP. Seeing is that FizzBuzz is a very simple program, and not a memory resource hog like the original benchmark shootout was, I was curious to see how something that simple would perform in a shootout.
I quickly modified the program and made it identical for Perl and PHP (other than the <php
tag at the top of the PHP version and the shebang line at the top of the Perl version). The results of the benchmark were surprising! Even with this ultra-simple example of exactly identical code, Perl was significantly (5.5x) faster.
1 2 3 4 |
|
1 2 3 4 |
|
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 |
|
The output of both scripts is identical.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|