我想将我的Python代码移植到C,但在此之前,我做了性能测试,但看起来并没有改善性能。Cython性能测试
首先C程序:
#include <stdio.h>
main()
{
printf("hello world \n");
}
[[email protected] c]$ gcc test.c
[[email protected] c]$ time ./a.out
hello world
real 0m0.001s
user 0m0.000s
sys 0m0.000s
二Python程序:
#!/usr/bin/env python
print "hello world \n"
[[email protected] c]$ time python test.py
hello world
real 0m0.024s
user 0m0.020s
sys 0m0.003s
三分之一用Cython ...
test.py
print "hello world \n"
[[email protected] c]$ cython --embed test.py
[[email protected] c]$ gcc $CFLAGS -I/usr/include/python2.6 -o test test.c -lpython2.6 -lpthread -lm -lutil -ldl
[[email protected] c]$ time ./test
hello world
real 0m0.024s
user 0m0.019s
sys 0m0.004s
所以对我来说,它看起来像用Cython没有真正提高任何表现。任何想法为什么以及如何我可以解决这个问题作为cython应该使python代码运行速度更快?
Cython删除字节码调度开销,并且如果您给它提供静态类型注释,有时可以删除Python对象。它不能神奇地将所有的Python代码编译成等效的'libc'调用,并且不能调用解释器的C API来实现大多数Python语义。试着比较一个循环增加十亿个整数。 – delnan 2012-02-20 12:56:42