php7 vs java8 vs nodejs5 vs lua5.2 计算性能比较

来源:互联网 发布:c语言标识符第一个字符 编辑:程序博客网 时间:2024/06/03 15:34

简单比较一下php7和java8的计算和字符串操作性能。

机器:osx 10.10   intel corei5  4GB 


php7.php:

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?php  
  2.   
  3. $t1 = microtime(true);  
  4.   
  5. for($i=0; $i<10000000; $i++){  
  6.     aaa($i);  
  7. }  
  8.   
  9. $t2 = microtime(true);  
  10.   
  11. echo 'php time:' . ($t2 - $t1)*1000 . "ms\n";  
  12.   
  13.   
  14. function aaa($i){  
  15.     $a = $i + 1;  
  16.     $b = 2.3;  
  17.     $s = "abcdefkkbghisdfdfdsfds";  
  18.   
  19.     if($a > $b){  
  20.         ++$a;  
  21.     }else{  
  22.         $b = $b + 1;  
  23.     }  
  24.   
  25.     if($a == $b){  
  26.         $b = $b + 1;  
  27.     }  
  28.   
  29.     $c = $a * $b + $a / $b - pow($a, 2);  
  30.     $d = substr($s, 0, strpos($s'kkb')) . strval($c);  
  31. }  
  32. ?>  

java8.java:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class Main  
  2. {  
  3.   
  4.     public static void main(String[] args)  
  5.     {  
  6.         long t1 = System.currentTimeMillis();  
  7.           
  8.         for(int i=0; i<10000000; i++){  
  9.             aaa((float)i);  
  10.         }  
  11.           
  12.         long t2 = System.currentTimeMillis();  
  13.           
  14.         System.out.println("java time:" + String.valueOf(t2 - t1) + "ms");  
  15.           
  16.     }  
  17.       
  18.     static void aaa(float i){  
  19.         float a = i + 1;  
  20.         float b = 2.3f;  
  21.         String s = "abcdefkkbghisdfdfdsfds";  
  22.           
  23.         if(a > b){  
  24.             ++a;  
  25.         }else{  
  26.             b = b + 1;  
  27.         }  
  28.   
  29.         if(a == b){  
  30.             b = b + 1;  
  31.         }  
  32.           
  33.         float c = (float) (a * b  + a / b - Math.pow(a, 2));  
  34.           
  35.         String d = s.substring(0, s.indexOf("kkb")) + String.valueOf(c);  
  36.     }  
  37. }  

node5.js:

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.       
  2.     var t1 = (new Date()).getTime();  
  3.   
  4.     for(var i=0; i<10000000; i++){  
  5.         aaa(i);  
  6.     }  
  7.       
  8.     var t2 = (new Date()).getTime();  
  9.       
  10.     console.log("nodejs time:" + (t2 - t1) + "ms");  
  11.           
  12.       
  13.     function aaa(i){  
  14.         var a = i + 1;  
  15.         var b = 2.3;  
  16.         var s = "abcdefkkbghisdfdfdsfds";  
  17.   
  18.         if(a > b){  
  19.             ++a;  
  20.         }else{  
  21.             b = b + 1;  
  22.         }  
  23.   
  24.         if(a == b){  
  25.             b = b + 1;  
  26.         }  
  27.   
  28.         var c = a  * b + a / b - Math.pow(a, 2);  
  29.           
  30.         var d = s.substring(0, s.indexOf("kkb")) + c.toString();  
  31.     }  

lua5.2.lua

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. function aaa(i)  
  2.      a = i + 1  
  3.      b = 2.3  
  4.      s = "abcdefkkbghisdfdfdsfds"  
  5.   
  6.     if(a > b) then  
  7.         a = a+1  
  8.     else  
  9.         b = b + 1  
  10.     end  
  11.   
  12.     if(a == b) then  
  13.         b = b + 1  
  14.     end  
  15.   
  16.      c = a  * b + a / b - math.pow(a, 2)  
  17.      d = string.sub(s, 0, string.find(s, "kkb")) .. tostring(c)  
  18. end  
  19.   
  20.   
  21. t1 = os.clock()  
  22.   
  23. for i=0, 10000000, 1 do  
  24.     aaa(i)  
  25. end  
  26.   
  27.  t2 = os.clock()  
  28.   
  29. print("lua time:" .. (t2 - t1) * 1000 .. "ms")  


分别执行1000万次计算,依次执行以下命令:

java -jar java8jar

node node5.js

php php7.php

luajit lua5.2.lua

lua lua5.2.lua


结果:



结论:由此可见就计算性能来说,java8 > nodejs5 > php7 > luajit > lua

java8是php7的5.2倍,nodejs5是php7的1.8倍,php7和luajit相当。

说lua是最快的脚本,那是往事了。静态语言的计算性能肯定比动态语言强很多。

对于密集计算,java是最好的选择;考虑到web的性能的瓶颈往往在数据库和IO,nodejs和php都是很好的选择。我个人喜欢php,无论从开发和部署,都比nodejs顺心点。

特别注意:如果function aaa(i)中没有参数i,那么nodejs是最快的,1000ms就完成了,估计nodejs对相同的执行结果进行了缓存。有好多人都犯了这个错误,测试下来就认为nodejs比java快多了。

0 0
原创粉丝点击