测试过程软硬件环境介绍:
endNum = 200000
ct = 0
for i in range(2, endNum+1):
base = 2
while base < i:
if 0 == i%base:
break
else:
base += 1
if base == i:
ct += 1
print ct
python版本查找素数代码运行三次,查找用时分别为:8m58.148s、8m54.101s和8m56.612s;
nodejs版本代码:
var endNum = 200000;
var ct = 0;
for(var i=2; i <= endNum; i++)
{
var base = 2;
while (base < i)
{
if (0 === i%base)
{
break;
}
else
{
base += 1;
}
}
if (base === i)
{
ct += 1;
}
}
console.log(ct);
nodejs版本查找素数代码运行三次,查找用时分别为:6.997s、7.000s和7.000s;
java版本代码:
public class ss{
public static void main(String[] args) {
int endNum = 200000;
int ct = 0;
for (int i = 2; i <= endNum; i++) {
int base = 2;
while (base < i)
{
if (0 == i%base)
{
break;
}
else
{
base += 1;
}
}
if (base == i)
{
ct += 1;
}
}
System.out.println(ct);
}
}
java版本查找素数代码运行三次,查找用时分别为:6.957s、6.959s和6.958s;
测试结果:java > nodejs 远远大于 python
综上所述,运行结果也看到,这个结果非常让我吃惊,真是万万没有想到,我猜到python的性能不及nodejs和java,但是没有想到性能差距会是这么大,java和nodejs性能是python的77倍之多
再来看看c语言版本:
#include
void main()
{
int endNum = 200000;
int ct = 0;
int base =0;
int i = 2;
for(i=2; i<=endNum; i++) {
base = 2;
while (base < i)
{
if (0 == i%base)
{
break;
}
else
{
base += 1;
}
}
if (base == i)
{
ct += 1;
}
}
printf("(%d)",ct);
}
C语言版本查找素数代码运行三次,查找用时分别为:6.886s、6.884s和6.886s;