admin管理员组文章数量:1435859
Cython,Python,C/C++的运行速度对比
关于Cython,Python,C/C++的运行速度的对比
测试环境:
处理器: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx 2.10 GHz
机带: RAM 8.00 GB
系统:Windows 10 21H2 (19044.1499)
(Cython 编译的指令为:python setup.py build_ext --inplace
测试结果如下
使用的语言 | 代码行数 | 运行线程数 | 句柄数 | 运行时间(s) |
---|---|---|---|---|
C/C++ | 46 | 2 | 60~62 | 6910.357 |
Python | 20 | 1 | 78 | UNKNOWN<程序崩溃> |
Cython | 4559(.c)+(UNKNOWN).pyd+1(import ***) | 4 | 84 | UNKNOWN<程序崩溃> |
下面给出测试的代码
C++:
#include<time.h>
#include<iostream>
#include<Windows.h>
#include<thread>
#pragma comment(lib,"winmm.lib")
int a, b, c, d;
void detect(void*);
int main()
{long long int sum = 0;a = 0, b = 0, c = 0, d = 0;_beginthread(detect, NULL, NULL);DWORD t1, t2;t1 = timeGetTime();for (a = 0; a < 1000; a++){for (b = 0; b < 1000; b++){for (c = 0; c < 1000; c++){for (d = 0; d < 1000; d++){sum = a + b + c + d;}}}}t2 = timeGetTime();std::cout << "time used:" << t2 - t1 << std::endl;system("pause");
}void detect(void*)
{while (1){if (a == 1000 && b == 1000 && c == 1000 && d == 1000){_endthread();}else{std::cout << "a=" << a << "b=" << b << "c=" << c << "d=" << d << "\n";system("cls");}}
}
下面给出Python的代码
import time
import os
a=0
b=0
c=0
d=0
def main():e=0t1=time.time()for a in range(0,1000):for b in range(0,1000):for c in range(0,1000):for d in range(0,1000):e=a+b+c+dos.system('cls')print('a=', a, 'b=', b, 'c=', c, 'd=', d)t2=time.time()print("time used:",t2-t1)
if __name__=='__main__':main()
Cython给出的代码太复杂了,这里就不写出来了
版权声明:本文标题:Cython,Python,CC++的运行速度对比 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1693719790a238835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论