如何使 python 更快?
How to make python faster?
我正在做一个 python 项目,需要在最短的时间内完成很多任务。
做了一些测试,print("Hello World!")
大约需要 0.7 秒到 运行 和 python。
在 c++ 中,cout<<"Hello World!";
大约需要 0.003 秒,与 python 相比有很大差异。
我应该采取什么方法来最大限度地减少 python 的 运行 时间?我应该如何编译代码?
这并没有真正回答问题,但表明(证明?)OP 的 Python 运行时设置/环境一定有问题
import time
start = time.perf_counter()
print('Hello world!')
end = time.perf_counter()
print(f'Duration={end-start:6f}s')
输出:
持续时间=0.000018s
我正在做一个 python 项目,需要在最短的时间内完成很多任务。
做了一些测试,print("Hello World!")
大约需要 0.7 秒到 运行 和 python。
在 c++ 中,cout<<"Hello World!";
大约需要 0.003 秒,与 python 相比有很大差异。
我应该采取什么方法来最大限度地减少 python 的 运行 时间?我应该如何编译代码?
这并没有真正回答问题,但表明(证明?)OP 的 Python 运行时设置/环境一定有问题
import time
start = time.perf_counter()
print('Hello world!')
end = time.perf_counter()
print(f'Duration={end-start:6f}s')
输出:
持续时间=0.000018s