lua和php运行速度
发布于: 2017-03-04 15:32分类: php,lua
在网上看到一篇文章,同样的功能用集中语言实现,然后执行比较速度。其中有我喜欢的php和正在学习的lua,就把代码搞到自己的机器上运行了,lua比php慢,但是luajit却快了十倍,让我开始喜欢luajit了。我喜欢一个语言,很简单,只需要一个简单的理由。
这是执行时间
php time:17811.112880707ms php5.6
lua time:31530ms lua5.14
luajit time:2790ms
检测你的nginx是否使用了jit
ldd /usr/local/openresty/nginx/sbin/nginx
下面上代码
<?php $t1 = microtime(true); for ($i = 0; $i < 10000000; $i++) { aaa($i); } $t2 = microtime(true); echo 'php time:' . ($t2 - $t1) * 1000 . "ms\n"; function aaa($i) { $a = $i + 1; $b = 2.3; $s = "abcdefkkbghisdfdfdsfds"; if ($a > $b) { ++$a; } else { $b = $b + 1; } if($a == $b){ $b = $b + 1; } $c = $a * $b + $a / $b - pow($a, 2); $d = substr($s, 0, strpos($s, 'kkb')) . strval($c); }
下面是lua代码
-- lua file --local os = os --local string = string --local tostring = tostring --local math = math local function aaa(i) a = i + 1 b = 2.3 s = "abcdefkkbghisdfdfdsfds" if(a > b) then a = a+1 else b = b + 1 end if(a == b) then b = b + 1 end c = a * b + a / b - math.pow(a, 2) d = string.sub(s, 0, string.find(s, "kkb")) .. tostring(c) end t1 = os.clock() for i=0, 10000000, 1 do aaa(i) end t2 = os.clock() print("lua time:" .. (t2 - t1) * 1000 .. "ms")
最近两个项目的api部分,我都采用了lua的openresty,这个测试结果让我更加有信息在以后的工作当中使用openresty。