test Environment: Processor i5-3230m,64 bit Ubuntu 14.04
Python 2.7.6, php 5.4.39, PHP 7.0.0-dev (2015/04/21)
Test content:
Bubble sort: Sorts 10 ascending numbers, outputs in descending order, and loops 1 million times.
Cumulative sum: 0+1+2+3+...+99999999
Bubble Sort Test results comparison:
Program: Python PHP5 PHP7
Time: 16.910s 14.715s 8.011s
Memory: 35.8m 9.0m 12.5m
When Python uses xrange instead, the memory consumption is 4.8MB and takes 16.784s.
Comparison of cumulative summation test results:
Program: Python PHP5 PHP7
Time: 10.057s 3.855s 1.855s
Memory:
3.039g8.9m 12.5m
When using range, Python memory consumption reaches 3GB, and after changing to xrange, memory consumption is 4.8MB and takes 9.460s.
Conclusion:
Both Python and PHP are dynamic scripting languages, and there is no JIT mechanism, so testing is fair.
Python computing performance is not as good as PHP5, and the PHP7 gap is even greater, so don't black PHP calculation is not as good as python.
PHP is compiled by itself, with many built-in features enabled, so the memory footprint in the test is more than Python.
The following is a detailed test procedure:
python bubble sort:
def bubble_sort (LST):
length = Len (LST)
For I in range (0, length, 1):
For j in range (0, length-1, 1):
If LST[J] < lst[j+1]:
temp = Lst[j]
LST[J] = lst[j+1]
LST[J+1] = Temp
return LST
For C in range (1000000):
LST = [0,1,2,3,4,5,6,7,8,9]
LST = Bubble_sort (LST)
Print (LST)
Test: Time Python bubblesort.py
Time: 0m16.910s
Memory: 35.8m
Python summation:
sum = 0
For I in Range (100000000):
sum + = i
Print (sum)
Test: Time Python sum.py
Time: 0m10.057s
Memory: 3.039g
php Bubble Sort:
<?php
function Bubble_sort ($array) {
$size = count ($array);
for ($i =0; $i < $size; $i + +) {
for ($j =0; $j < $size-1; $j + +) {
if ($array [$j] < $array [$j +1]) {
$temp = $array [$j];
$array [$j] = $array [$j +1];
$array [$j +1] = $temp;
}
}
}
return $array;
}
for ($c =0; $c <1000000; $c + +) {
$array = Array (0,1,2,3,4,5,6,7,8,9);
$array = Bubble_sort ($array);
}
Print_r ($array);
Test: Time PHP bubblesort.php
Time: 0m14.715s
Memory: 9.0m
Test: Time Phpng bubblesort.php
Time: 0m8.011s
Memory: 12.5m
php Cumulative sum:
<?php
$sum = 0;
for ($i =0; $i <100000000; $i + +)
$sum + = $i;
Print_r ($sum);
Test: Time PHP sum.php
Time: 0m3.855s
Memory: 8.9m
Test: Time Phpng sum.php
Time: 0m1.855s
Memory: 12.5m
Corrections:
Although the algorithm in the text can be sorted, but did some useless.
The bubble sorting algorithm in this paper is incorrect, and the second loop should be for ($j =0; $j <
$size-$i; $j + +).
function Bubble_sort ($array) {
$size = count ($array);
for ($i =0; $i < $size; $i + +) {
for ($j =0; $j < $size-$i; $j + +) {
if ($array [$j] < $array [$j +1]) {
$temp = $array [$j];
$array [$j] = $array [$j +1];
$array [$j +1] = $temp;
}
}
}
return $array;
}
Bubble Sort Reference:
Http://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Bubble_sort#PHP
Python vs PHP bubble Sorting and summation sum calculation performance test