C++ vs. PHP
I have decided to compare the performance of c++ against php using same logical code. Its obvious which would run faster, but how much faster is what I wanted to know. It can be said that c++ is compiled binary and php is a interpreted scripting language unfair and are two totally different tiers of languages, but since web applications can be written using php and c++ I regarded them as being at lease somewhat relevant. I am using gcc 3.4.2 and php 4.3.10 for this and time to see how much CPU time each takes. I did each part 3 times, and below are the averages, in the order of c++ vs php = php/c++. Note that syst times are not relevent, but I included them anyway. This is in no way a balanced all rounded bench mark between the performance’s of php and c++, but you get the idea.
Results
Part 1 - For loop from 0 - 999999
real : 0.01000 vs 0.89133 = 89.1330000
user : 0.00600 vs 0.60667 = 101.111667
syst : 0.00233 vs 0.02667 = 11.4463519
Part 2 - For loop outputting 0 - 99999 to stdout
real : 4.46367 vs 4.77933 = 1.07071759
user : 0.04866 vs 0.29000 = 5.95972051
syss : 0.08366 vs 0.25233 = 3.01613674
Part 3 - For loop outputting 0 - 999999 to file
real : 0.49400 vs 5.28133 = 10.6909514
user : 0.43700 vs 2.84867 = 6.51869565
syss : 0.03067 vs 2.37233 = 77.3501793
Part 4 - For loop executing a shell command 999 times
real : 7.01067 vs 9.60100 = 1.36948394
user : 2.89033 vs 3.10000 = 1.07254189
syss : 3.33100 vs 5.75767 = 1.72851096
Conclusion
C++ outdid php in every single test. The least performance gain c++ took over php was in outputting the loop results into stdout (when its printing out stuff) with a factor of ~1.07 and c++ outdid php the most in just running through a loop (doing sorting, internal calculations) by c++ doing it almost 90 times faster than php.