Cython性能测试

kri*_*itx 1 python linux cython

我试图将我的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
Run Code Online (Sandbox Code Playgroud)

第二个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
Run Code Online (Sandbox Code Playgroud)

第三次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
Run Code Online (Sandbox Code Playgroud)

所以对我来说,看起来cython并没有真正改善任何性能.任何想法为什么以及如何解决这个问题,因为cython应该让python代码运行得更快?

orl*_*rlp 13

你在这里看到的是没有真正的性能测试.您只在程序中执行一个操作.

完整的执行时间无效.现在执行时间唯一重要的是开销.启动过程的开销,以及启动解释器的Python案例开销.

最后,您正在测试I/O的性能.这本身是一件非常棘手的事情,因为I/O的性能通常不受编程语言的限制,而是受操作系统的限制.

  • 准确的回复 - 如果您添加一个示例来正确测试cython和c代码以及结果,我会投票.我自己做,但我在Windows机器上,似乎无法让Cython编译...... (2认同)