C比Python慢​​? [英] C Slower than Python?

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

问题描述

我一直在用自己的宠物语言对Python进行基准测试,以便操纵

短字符串。这测试了字符串的表达式a = b + c,而Python

代码如下:


b =" abc"

c =" xyz"
$ x $ b for i in xrange(10000000):

a = b + c

print" A ="一个


在我的机器上用Python 2.5花了大约2.5秒(我自己的努力

达到0.7秒......)

>
相当不错,但C能做多快?我预计这两个都会被敲掉,但是下面的代码花了4秒钟(m -w为3.4.5,-O2)。


(Timings for更长的60-chars字符串为3.5秒,Python为7.5秒

为C.所有时间都是经过的时间)


好​​的,这段代码天真而简单,但是你怎么用C做呢?

(顺便说一下,我省略了malloc检查,这是我自己的代码,我认为在Python中是
。 )


/ *评估字符串a = b + c 10m倍* /


#include< stdio.h>

#include< stdlib.h>

#include< string.h>

#include< time.h>


int main(void){

int i,t;

char * a = NULL;

char * b =" abc";

char * c =" xyz";


t = clock();


for(i = 0; i< 10000000; ++ i){

free(a);

a = malloc(strlen(b)+ strlen(c) +1);

strcpy(a,b);

strcat(a,c);

}


printf(" Result =%s \ n",a);


printf(" Time:%d \ n",clock() - t);

}


-

Bartc

I''d been benchmarking my own pet language against Python for manipulation of
short strings. This tested the expression a=b+c for strings, and the Python
code looks like:

b="abc"
c="xyz"
for i in xrange(10000000):
a=b+c
print "A=",a

This took about 2.5 secs with Python 2.5 on my machine (my own efforts
achieved 0.7 secs..)

Pretty good, but how fast could C do it? I expected both of these to be
thrashed, yet the code below took over 4 seconds (mingw 3.4.5 with -O2).

(Timings for longer 60-chars strings were 3.5 secs for Python and 7.5 secs
for C. All timings are elapsed time)

OK, this code is naive and simplistic, but how else would you do it in C?
(BTW I''ve omitted malloc checking, which is in my own code and I presume is
in Python.)

/* Evaluate string a=b+c 10m times */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main(void) {
int i,t;
char *a=NULL;
char *b="abc";
char *c="xyz";

t=clock();

for (i=0; i<10000000; ++i) {
free(a);
a=malloc(strlen(b)+strlen(c)+1);
strcpy(a,b);
strcat(a,c);
}

printf("Result=%s\n",a);

printf("Time: %d\n",clock()-t);
}

--
Bartc

推荐答案

Bartc说:
Bartc said:

我一直在用自己的宠物语言对Python进行操作,以便进行操作

短串。这测试了字符串的表达式a = b + c,并且

Python代码如下:


b =" abc"

c =" xyz"
$ x $ b for i in xrange(10000000):

a = b + c

print" A ="一个


在我的机器上用Python 2.5花了大约2.5秒(我自己的努力

达到0.7秒......)

>
相当不错,但C能做多快?
I''d been benchmarking my own pet language against Python for manipulation
of short strings. This tested the expression a=b+c for strings, and the
Python code looks like:

b="abc"
c="xyz"
for i in xrange(10000000):
a=b+c
print "A=",a

This took about 2.5 secs with Python 2.5 on my machine (my own efforts
achieved 0.7 secs..)

Pretty good, but how fast could C do it?



以下是ISO C程序的一些时序,它产生相同的输出。


me @ heretime ./foo

abcxyz


真实0m0.015s

用户0m0.000s

sys 0m0.010s

me @ heretime ./foo / dev / null

real 0m0.003s

用户0m0.000s

sys 0m0.000s

me @ herecat foo.c

#include< stdio.h>


int main (无效)

{

printf(" abcxyz \ n");


返回0;

}


您可能需要提出更好的问题。


-

Richard Heathfield < http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - 1999年7月29日dmr

Here are some timings for an ISO C program which produces the same output.

me@heretime ./foo
abcxyz

real 0m0.015s
user 0m0.000s
sys 0m0.010s
me@heretime ./foo /dev/null

real 0m0.003s
user 0m0.000s
sys 0m0.000s
me@herecat foo.c
#include <stdio.h>

int main(void)
{
printf ("abcxyz\n");

return 0;
}

You might need to ask a better question.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


文章< RN ******************* @ text.news.virginmedia .com>,

Bartc< bc@freeuk.comwrote:
In article <RN*******************@text.news.virginmedia.com >,
Bartc <bc@freeuk.comwrote:

>我一直在使用自己的宠物语言进行基准测试反对Python操纵
短字符串。
>I''d been benchmarking my own pet language against Python for manipulation of
short strings.



您的测试几乎肯定主要是测量内存分配

速度。 Python可能使用比
malloc()更专业的分配器,而malloc()在实现之间差异很大。


(在我的电脑上,C程序比Python快3倍左右。)


- Richard

-

请记得提及我/ in你留下的录音带。

Your tests are almost certainly primarily measuring memory allocation
speed. Python may well used a more specialised allocator than
malloc(), and malloc() varies greatly between implementations.

(On my computer, the C program is about 3 times faster than the Python.)

-- Richard
--
Please remember to mention me / in tapes you leave behind.


10月8日下午2:26,Bartc < b ... @ freeuk.comwrote:
On Oct 8, 2:26 pm, "Bartc" <b...@freeuk.comwrote:

我一直在使用我自己的宠物语言对Python进行基准操作来操纵

短串。这测试了字符串的表达式a = b + c,而Python

代码如下:


b =" abc"

c =" xyz"
$ x $ b for i in xrange(10000000):

a = b + c

print" A ="一个


在我的机器上用Python 2.5花了大约2.5秒(我自己的努力

达到0.7秒......)

>
相当不错,但C能做多快?我预计这两个都会被敲掉,但是下面的代码花了4秒钟(m -w为3.4.5,-O2)。


(Timings for更长的60-chars字符串为3.5秒,Python为7.5秒

为C.所有时间都是经过的时间)


好​​的,这段代码天真而简单,但是你怎么用C做呢?

(顺便说一下,我省略了malloc检查,这是我自己的代码,我认为在Python中是
。 )
I''d been benchmarking my own pet language against Python for manipulation of
short strings. This tested the expression a=b+c for strings, and the Python
code looks like:

b="abc"
c="xyz"
for i in xrange(10000000):
a=b+c
print "A=",a

This took about 2.5 secs with Python 2.5 on my machine (my own efforts
achieved 0.7 secs..)

Pretty good, but how fast could C do it? I expected both of these to be
thrashed, yet the code below took over 4 seconds (mingw 3.4.5 with -O2).

(Timings for longer 60-chars strings were 3.5 secs for Python and 7.5 secs
for C. All timings are elapsed time)

OK, this code is naive and simplistic, but how else would you do it in C?
(BTW I''ve omitted malloc checking, which is in my own code and I presume is
in Python.)



那么问题是什么?你应该知道比在这里发布

基准测试的东西更好。

So WHERE''s the C question? You should know better than posting
benchmarking stuff here.


/ *评估字符串a = b + c 10m倍* /


#include< stdio.h>

#include< stdlib.h>

#include< string.h>

#include< time.h>


int main(无效){

int i,t;

char * a = NULL;

char * b =" abc";

char * c =" xyz";


t = clock();
/* Evaluate string a=b+c 10m times */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main(void) {
int i,t;
char *a=NULL;
char *b="abc";
char *c="xyz";

t=clock();



t是一个整数。 clock返回clock_t,这是一个算术类型,

但不一定是int。

它可能是long,或者是unsigned;在这种情况下,取决于值

,您可以调用 impl-defined行为或提出impl-defined
信号。

它可能是一个浮点数,在这种情况下你会失去信息。 (如果

值大于INT_MAX或小于INT_MIN,那么我之前说的是


t is an integer. clock returns clock_t, which is an arithmetic type,
but not necessarily an int.
It could be a long, or unsigned; in which case depending on the value
you may be "invoking" impl-defined behavior or raising an impl-defined
signal.
It could be a float, in which case you''d lose information. (and if the
value is bigger than INT_MAX or less than INT_MIN, also what I said
before)


for(i = 0; i< 10000000; ++ i){
for (i=0; i<10000000; ++i) {



如果INT_MAX< 10000000,无限循环,然后你调用undefined

行为。 (整数溢出)

If INT_MAX < 10000000, infinite loop and then you invoke undefined
behavior. (integer overflow)


free(a);

a = malloc(strlen(b)+ strlen(c)+1);
free(a);
a=malloc(strlen(b)+strlen(c)+1);



如果malloc返回NULL怎么办?你不在乎吗?

What if malloc returns NULL? You don''t care?


strcpy(a,b);

strcat(a,c);
strcpy(a,b);
strcat(a,c);



不是实际的错误:如果b是一个零长度的字符串,你的代码就不会是b $ b工作了。 (它显然不在你的代码中,但我只是提到这个

以防你决定让用户选择字符串)

Not an actual bug: if b is a zero length string your code doesn''t
work. (it obviously isn''t in your code, but I''m only mentioning this
in case you decide to let the user choose the strings)


}


printf(" Result =%s \ n",a);


printf("时间:%d \ n",clock() - t);
}

printf("Result=%s\n",a);

printf("Time: %d\n",clock()-t);



如果时钟返回任何大于int的整数类型,或浮点

类型,则调用UB。

If clock returns any integer type larger than int, or a floating point
type, you invoke UB.


>

}
>
}



如果您打算实施时间,请使用合作

计划。当你的程序

不正确时,责备其输出的实现是非常愚蠢的。

此外,如果你要发布你的结果,请在

适当的组,而不是comp.lang.c.

If you''re going to time implementations, do it with CONFORMING
programs. Blaming the implementation for its output when your program
is incorrect is plain silly.
Moreover, if you''re going to post your results, please do it in an
appropriate group, not comp.lang.c.


这篇关于C比Python慢​​?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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