gpt4 book ai didi

python - Cython 性能测试

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:14:35 25 4
gpt4 key购买 nike

我正在尝试将我的 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 代码运行得更快?

最佳答案

您在这里看到的不是真正的性能测试。您只在程序中执行一个微小操作。

完整的执行时间无效。现在执行时间中唯一重要的是开销。进程启动的开销,在 Python 的情况下,启动解释器的开销。

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

关于python - Cython 性能测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9361407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com