0
点赞
收藏
分享

微信扫一扫

Cython,Python,C/C++的运行速度对比

山竹山竹px 2022-01-22 阅读 71

关于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
测试结果如下

使用的语言代码行数运行线程数句柄数运行时间
C/C++46260~62UNKNOWN
Python20178UNKNOWN
Cython4559(.c)+(UNKNOWN).pyd+1(import ***)UNKNOWNUNKNOWNUNKNOWN

下面给出测试的代码
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=0
    t1=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+d
                    os.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 条评论