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

左手的ㄟ右手 2022-07-20 13:07 390阅读 0赞

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

机器:osx 10.10 intel corei5 4GB

php7.php:

[php] view plain copy

在CODE上查看代码片 派生到我的代码片

  1. <?php
  2. $t1 = microtime(true);
  3. for($i=0; $i<10000000; $i++){
  4. aaa($i);
  5. }
  6. $t2 = microtime(true);
  7. echo’php time:’ . ($t2 - $t1)*1000 . “ms\n”;
  8. function aaa($i){
  9. $a = $i + 1;
  10. $b = 2.3;
  11. $s = “abcdefkkbghisdfdfdsfds”;
  12. if($a > $b){
  13. ++$a;
  14. }else{
  15. $b = $b + 1;
  16. }
  17. if($a == $b){
  18. $b = $b + 1;
  19. }
  20. $c = $a * $b + $a / $b - pow($a, 2);
  21. $d = substr($s, 0, strpos($s, ‘kkb’)) . strval($c);
  22. }
  23. ?>

java8.java:

[java] view plain copy

在CODE上查看代码片 派生到我的代码片

  1. public**class** Main
  2. {
  3. public**static**void main(String[] args)
  4. {
  5. long t1 = System.currentTimeMillis();
  6. for(int i=0; i<10000000; i++){
  7. aaa((float)i);
  8. }
  9. long t2 = System.currentTimeMillis();
  10. System.out.println(“java time:” + String.valueOf(t2 - t1) + “ms”);
  11. }
  12. static**void aaa(float** i){
  13. float a = i + 1;
  14. float b = 2.3f;
  15. String s = “abcdefkkbghisdfdfdsfds”;
  16. if(a > b){
  17. ++a;
  18. }else{
  19. b = b + 1;
  20. }
  21. if(a == b){
  22. b = b + 1;
  23. }
  24. float c = (float) (a * b + a / b - Math.pow(a, 2));
  25. String d = s.substring(0, s.indexOf(“kkb”)) + String.valueOf(c);
  26. }
  27. }

node5.js:

[javascript] view plain copy

在CODE上查看代码片 派生到我的代码片

  1. var t1 = (new Date()).getTime();
  2. for(var i=0; i<10000000; i++){
  3. aaa(i);
  4. }
  5. var t2 = (new Date()).getTime();
  6. console.log(“nodejs time:” + (t2 - t1) + “ms”);
  7. function aaa(i){
  8. var a = i + 1;
  9. var b = 2.3;
  10. var s = “abcdefkkbghisdfdfdsfds”;
  11. if(a > b){
  12. ++a;
  13. }else{
  14. b = b + 1;
  15. }
  16. if(a == b){
  17. b = b + 1;
  18. }
  19. var c = a * b + a / b - Math.pow(a, 2);
  20. var d = s.substring(0, s.indexOf(“kkb”)) + c.toString();
  21. }

lua5.2.lua

[plain] view plain copy

在CODE上查看代码片 派生到我的代码片

  1. function aaa(i)
  2. a = i + 1
  3. b = 2.3
  4. s = “abcdefkkbghisdfdfdsfds”
  5. if(a > b) then
  6. a = a+1
  7. else
  8. b = b + 1
  9. end
  10. if(a == b) then
  11. b = b + 1
  12. end
  13. c = a * b + a / b - math.pow(a, 2)
  14. d = string.sub(s, 0, string.find(s, “kkb”)) .. tostring(c)
  15. end
  16. t1 = os.clock()
  17. for i=0, 10000000, 1 do
  18. aaa(i)
  19. end
  20. t2 = os.clock()
  21. 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

结果:

20160227105143760

结论:由此可见就计算性能来说,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 条评论,390人围观)

还没有评论,来说两句吧...

相关阅读

    相关 lua table vs closure

      最近在重构自己写的框架中的定时器模块,需要把回调函数保存起来,大概如下: function timer_mgr:save_timer( this,callback