速度比较Perl Python& C [英] Speed Comparison Perl Python & C

查看:73
本文介绍了速度比较Perl Python& C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个周末只是鬼混。用C语言编写和定时程序,Perl和

Python。每个程序计数到1,000,000并将每个数字打印到

控制台。我有点惊讶。我不是专家C或Perl

编程练习,我最熟悉Python,但也可以使用其他的




以下是我的结果:


C = 23秒

Python = 26.5秒

Perl = 34.5秒


以下是程序:


------------------ -------

#C版:

---------------------- ---


#include< stdio.h>


int main(无效)

{

int x = 0;

while(x <1000000){

printf("%d \ n",x ++);

}

}


-------------------- -----

#Python版本:

------------------------ -


#!/ usr / bin / python

x = 0

而x< 1000000:

x = x + 1

打印x


-------------- -----------

#Perl版本:

------------------ -------


#!/ usr / bin / perl -Tw


使用严格;


我的$ x = 0;

而($ x <1000000){

print $ x ++," \\\
" ;;

}


你们怎么看待这个?关于Perl& amp;我不太了解C,和

也许是Python,要知道这确实是一个公平的测试。我认为C会比这更快地做到这一点。有什么想法吗?

Just fooling around this weekend. Wrote and timed programs in C, Perl and
Python. Each Program counts to 1,000,000 and prints each number to the
console as it counts. I was a bit surprised. I''m not an expert C or Perl
programming expery, I''m most familiar with Python, but can use the others
as well.

Here are my results:

C = 23 seconds
Python = 26.5 seconds
Perl = 34.5 seconds

Here are the programs:

-------------------------
#The C version:
-------------------------

#include <stdio.h>

int main(void)
{
int x = 0;
while (x < 1000000) {
printf("%d \n", x++);
}
}

-------------------------
#The Python version:
-------------------------

#!/usr/bin/python

x = 0
while x < 1000000:
x = x + 1
print x

-------------------------
#The Perl version:
-------------------------

#!/usr/bin/perl -Tw

use strict;

my $x = 0;
while($x < 1000000) {
print $x++, "\n";
}

What do you guys think of this? I don''t know enough about Perl & C, and
perhaps Python, to know if this was indeed a fair test. I thought C would
do this much faster than it did. Any ideas?


推荐答案

x = 0;

while(
x = 0;
while(


x <1000000){

print
x < 1000000) {
print


x ++," \ n";

}


你们怎么看待这个?关于Perl& amp;我不太了解C,和

也许是Python,要知道这确实是一个公平的测试。我认为C会比这更快地做到这一点。有什么想法吗?


x++, "\n";
}

What do you guys think of this? I don''t know enough about Perl & C, and
perhaps Python, to know if this was indeed a fair test. I thought C would
do this much faster than it did. Any ideas?



这篇关于速度比较Perl Python&amp; C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆