我正在尝试将我的python代码移植到C,但在此之前我做了一个性能测试,但看起来它并没有提高性能。
首先是C程序:
#include <stdio.h>
main ()
{
printf("hello world \n");
}
[root@server c]$ gcc test.c
[root@server c]$ time ./a.out
hello world
real 0m0.001s
user 0m0.000s
sys 0m0.000s第二个是Python程序:
#!/usr/bin/env python
print "hello world \n"
[root@server c]$ time python test.py
hello world
real 0m0.024s
user 0m0.020s
sys 0m0.003s第三个Cython..。
test.py
print "hello world \n"
[root@server c]$ cython --embed test.py
[root@server c]$ gcc $CFLAGS -I/usr/include/python2.6 -o test test.c -lpython2.6 -lpthread -lm -lutil -ldl
[root@server c]$ time ./test
hello world
real 0m0.024s
user 0m0.019s
sys 0m0.004s所以在我看来,cython并没有真正提高任何性能。你知道为什么以及如何解决这个问题吗?因为cython应该能让python代码运行得更快。
发布于 2012-02-20 20:53:16
您在这里看到的并不是真正的性能测试。你在你的程序中只做了一个tiny操作。
完整的执行时间是空的。现在,执行时间中唯一重要的事情就是开销。进程启动的开销,在Python的情况下,还有启动解释器的开销。
最后,您在这里测试I/O的性能。这本身就是一件非常棘手的事情,因为I/O的性能通常不受编程语言的限制,而是受操作系统的限制。
https://stackoverflow.com/questions/9361407
复制相似问题