简单比较一下php7和java8的计算和字符串操作性能。
机器:osx 10.10 intel corei5 4GB
php7.php:
$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);
}
?>
public class Main
{
public static void main(String[] args)
{
long t1 = System.currentTimeMillis();
for(int i=0; i<10000000; i++){
aaa((float)i);
}
long t2 = System.currentTimeMillis();
System.out.println("java time:" + String.valueOf(t2 - t1) + "ms");
}
static void aaa(float i){
float a = i + 1;
float b = 2.3f;
String s = "abcdefkkbghisdfdfdsfds";
if(a > b){
++a;
}else{
b = b + 1;
}
if(a == b){
b = b + 1;
}
float c = (float) (a * b + a / b - Math.pow(a, 2));
String d = s.substring(0, s.indexOf("kkb")) + String.valueOf(c);
}
}
node5.js:
var t1 = (new Date()).getTime();
for(var i=0; i<10000000; i++){
aaa(i);
}
var t2 = (new Date()).getTime();
console.log("nodejs time:" + (t2 - t1) + "ms");
function aaa(i){
var a = i + 1;
var b = 2.3;
var s = "abcdefkkbghisdfdfdsfds";
if(a > b){
++a;
}else{
b = b + 1;
}
if(a == b){
b = b + 1;
}
var c = a * b + a / b - Math.pow(a, 2);
var d = s.substring(0, s.indexOf("kkb")) + c.toString();
}
lua5.2.lua
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")
分别执行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快多了。