python,ruby,php分别求素数程序--速度对比

python,ruby,php分别求素数程序--速度对比
所用系统:win7 64位
====================================================================
1.1 python程序
f=open('time0.txt','w')
f.close
print(2,' ',end='')
for i in range(3,300000):
 for j in range(2,i+1):
  if i%j==0:break
 if j==i:
  print(i,' ',end='')
f=open('time1.txt','w')
f.close
结果:
2 3 5 7 11 13 17 19...299983 299993

 D:\Python30 的目录

2019/10/11  14:51                 0 time0.txt
2019/10/11  15:17                 0 time1.txt

历时26分钟
====================================================================
1.2 ruby程序
f=File.new("time0.txt","w")
f.close
print 2," "
for i in 3..300000
 for j in 2..i
   break if i%j==0
 end
 next if j==i
 print i," "
end
f=File.new("time1.txt","w")
f.close
结果:
2 3 5 7 11 13 17 19...299983 299993

 D:\ruby200 的目录

2019/10/11  14:51                 0 time0.txt
2019/10/11  15:14                 0 time1.txt

历时23分钟
====================================================================
1.3 PHP程序
<?php
$f=fopen("time0.txt","w");
fclose($f);
echo "2 ";
for($i=3;$i<300000;$i++){
 for($j=2;$j<$i+1;$j++){
  if ($i%$j==0){
   break;
  }
 }
 if ($j<$i){
  continue;
 }
 echo $i.' ';
}
$f=fopen("time1.txt","w");
fclose($f);
?>
(在命令行方式下运行)
结果:
2 3 5 7 11 13 17 19...299983 299993

 C:\wamp\bin\php\php5.4.3 的目录

2019/10/11  14:59                 0 time0.txt
2019/10/11  15:07                 0 time1.txt

历时8分钟!!!
====================================================================
====================================================================
====================================================================
  考虑到print语句可能花费时间不同,只关注计算所需时间,去掉print语句
(在php程序中相应去掉echo语句).
2.1 python程序
f=open('time0.txt','w')
f.close
#print(2,' ',end='')
for i in range(3,300000):
 for j in range(2,i+1):
  if i%j==0:break
 if j<i:continue
 #print(i,' ',end='')
f=open('time1.txt','w')
f.close

 D:\Python30 的目录

2019/10/11  15:29                 0 time0.txt
2019/10/11  15:51                 0 time1.txt

历时21分钟
====================================================================
2.2 ruby程序
f=File.new("time0.txt","w")
f.close
#print 2," "
for i in 3..300000
 for j in 2..i
   break if i%j==0
 end
 next if j<i
 #print i," "
end
f=File.new("time1.txt","w")
f.close
结果:

 D:\ruby200 的目录

2019/10/11  15:41                 0 time0.txt
2019/10/11  15:52                 0 time1.txt

历时11分钟
====================================================================
2.3 PHP程序
<?php
$f=fopen("time0.txt","w");
fclose($f);
//echo "2 ";
for($i=3;$i<300000;$i++){
 for($j=2;$j<$i+1;$j++){
  if ($i%$j==0){
   break;
  }
 }
 if ($j<$i){
  continue;
 }
 //echo $i.' ';
}
$f=fopen("time1.txt","w");
fclose($f);
?>
(在命令行方式下运行)
结果:

 C:\wamp\bin\php\php5.4.3 的目录

2019/10/11  15:09                 0 time0.txt
2019/10/11  15:16                 0 time1.txt

历时7分钟!!!
====================================================================
  结论:
   php大大快于ruby快于python
   速度之比:
    php       
   ----=2.8, 
   ruby
    php       
   ----=3.25. 
   python


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值