Home  >  Article  >  Backend Development  >  NodeJs and PHP benchmark

NodeJs and PHP benchmark

藏色散人
藏色散人forward
2019-11-13 14:20:222911browse

This benchmark test is just to simply measure the time loss of node and php in bubble sorting. The basic idea is to use bubble sorting to calculate the average value after each operation 100 times; the bubble sorting algorithm is taken from the Internet, and the test code As follows:

Code

JavaScript:

function sort(arr){
    var n=arr.length; //获取数组的长度,即有n个数在排序
    var temp=null; //定义一个临时变量,交换数据用
    for(var i=0; i<n-1; i++){ //外层循环n-1次
        for(var j=0; j<n-1-i; j++){ //内层每次循环n-1-i次,每次循环完,都能从剩下的数当中找出一个最大的放在n-1-i的位置
            if(arr[j]>arr[j+1]){ //如果a[j]>a[j+1]则交换位置
                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;
            }
        }
    }
    return arr; //返回排好序的数组
}
const array = [49, 38, 65, 97, 76, 13, 27,49, 38, 65, 97, 76, 13, 27];
const startTime = new Date();
for (let i = 0; i <= 99; i++) {
    const arr = sort(array);
}
console.log((new Date() - startTime) / 1000  / 100);

PHP:

function bubble_sort($array)
{
    $count = count($array);
    if ($count <= 0) return false;
    for ($i = 0; $i < $count; $i++) {
        for ($j = $count - 1; $j > $i; $j--) {
            //如果后一个元素小于前一个,则调换位置
            if ($array[$j] < $array[$j - 1]) {
                $tmp = $array[$j];
                $array[$j] = $array[$j - 1];
                $array[$j - 1] = $tmp;
            }
        }
    }
    return $array;
}
$array = [49, 38, 65, 97, 76, 13, 27,49, 38, 65, 97, 76, 13, 27];
$startTime = microtime(true);
for ($i = 0; $i <= 99; $i++) {
    $arr = bubble_sort($array);
}
echo number_format((microtime(true) - $startTime) / 100, 10);

Note: In order to test php5, the emphasized syntax type of php7 was not used.

Test results

As shown in the figure:

PHP5.6

NodeJs and PHP benchmark

Node7.9:

NodeJs and PHP benchmark

PHP7.1

NodeJs and PHP benchmark

Conclusion

The results of the three calculations are:

NodeJs and PHP benchmark

Testing machine

NodeJs and PHP benchmark

Judging from the results of this test, in terms of execution speed, PHP7.1 > PHP5.6 > Node7.9

The above is the detailed content of NodeJs and PHP benchmark. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete