最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

Cython,Python,CC++的运行速度对比

IT圈 admin 43浏览 0评论

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++46260~626910.357
Python20178UNKNOWN<程序崩溃>
Cython4559(.c)+(UNKNOWN).pyd+1(import ***)484UNKNOWN<程序崩溃>

下面给出测试的代码
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,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++46260~626910.357
Python20178UNKNOWN<程序崩溃>
Cython4559(.c)+(UNKNOWN).pyd+1(import ***)484UNKNOWN<程序崩溃>

下面给出测试的代码
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给出的代码太复杂了,这里就不写出来了

发布评论

评论列表 (0)

  1. 暂无评论