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

你可能感兴趣的:(C++,python,c++,python,c语言)