KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Currently I want to compare the speed of Python and C when they're used to do string stuff. I think C should give better performance than Python will; however, I got a total contrary result. Here's the C program: #include <unistd.h> #include <sys/time.h> #define L (100*1024) char s[L+1024]; char c[2*L+1024]; double time_diff( struct timeval et, struct timeval st ) { return 1e-6*((et.tv_sec - st.tv_sec)*1000000 + (et.tv_usec - st.tv_usec )); } int foo() { strcpy(c,s); strcat(c+L,s); return 0; } int main() { struct timeval st; struct timeval et; int i; //printf("s:%x\nc:%x\n", s,c); //printf("s=%d c=%d\n", strlen(s), strlen(c)); memset(s, '1', L); //printf("s=%d c=%d\n", strlen(s), strlen(c)); foo(); //printf("s=%d c=%d\n", strlen(s), strlen(c)); //s[1024*100-1]=0; gettimeofday(&st,NULL); for( i = 0 ; i < 1000; i++ ) foo(); gettimeofday(&et,NULL); printf("%f\n", time_diff(et,st)); return 0; } and this is the Python one: import time s = '1'*102400 def foo(): c = s + s #assert( len(c) == 204800 ) st = time.time() for x in xrange(1000): foo() et = time.time() print (et-st) and what I get: root@xkqeacwf:~/lab/wfaster# python cp100k.py 0.027932882309 root@xkqeacwf:~/lab/wfaster# gcc cp100k.c root@xkqeacwf:~/lab/wfaster# ./a.out 0.061820 Does that make sense? Or am I just making any stupid mistakes?
Tags (comma-separated)
Save Edits
Cancel