[英]python code order of magnitude faster than compiled C code for simple import statement
我对以下两个语义等效(?)程序的性能差异感到困惑。
Python:
#test.py
import time
starttime=time.time()
import tensorflow
print(f"Import took: {time.time() - starttime}")
C 使用 CPython
//test.c
#include <Python.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
Py_Initialize();
clock_t t = clock();
PyObject* pModule = PyImport_ImportModule("tensorflow");
double time_taken = ((double)clock() - t)/CLOCKS_PER_SEC;
printf("Import took: %f\n", time_taken);
return 0;
}
现在比较两者:
cl.exe /I"C:\Program Files\Python37\include" test.c
link test.obj python37.lib /LIBPATH:"C:\Program Files\Python37\libs"
.\test.exe
2020-04-17 13:00:51.598550:W tensorflow/stream_executor/platform/default/dso_loader.cc:55] 无法加载动态库“cudart64_100.dll”; dlerror: cudart64_100.dll not found 2020-04-17 13:00:51.606296: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
进口拍:23.160523891448975
python test.py
2020-04-17 13:01:19.525648:W tensorflow/stream_executor/platform/default/dso_loader.cc:55] 无法加载动态库“cudart64_100.dll”; dlerror: cudart64_100.dll not found 2020-04-17 13:01:19.530726: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
进口拍:2.3172824382781982
没关系,但我的 tensorflow 版本是 1.15 为什么 python VM 代码比编译的 CPython 代码快得多? python vm 是否添加了 PyImport_ImportModule 没有的一些魔力?
PyImport_ImportModule 是 C ABI 的一部分,这意味着它可能会受到性能损失: https://docs.python.org/3/chtml-list/stable-abi-list/stable-abi。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.