简体   繁体   English

用于简单导入语句的 python 代码比编译的 C 代码快一个数量级

[英]python code order of magnitude faster than compiled C code for simple import statement

I'm baffled by the performance difference of the following two semantically equivalent(?) programs.我对以下两个语义等效(?)程序的性能差异感到困惑。

Python: Python:

#test.py
import time
starttime=time.time()
import tensorflow 
print(f"Import took: {time.time() - starttime}")

C using CPython 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;
}

Now compare the two:现在比较两者:

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] Could not load dynamic library 'cudart64_100.dll'; 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. 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.

Import took: 23.160523891448975进口拍:23.160523891448975

python test.py

2020-04-17 13:01:19.525648: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_100.dll'; 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. 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.

Import took: 2.3172824382781982进口拍:2.3172824382781982

Not that it should matter much but my tensorflow version is 1.15 Why is the python VM code so much faster than the compiled CPython code?没关系,但我的 tensorflow 版本是 1.15 为什么 python VM 代码比编译的 CPython 代码快得多? Does the python vm add some magic that PyImport_ImportModule doesn't? python vm 是否添加了 PyImport_ImportModule 没有的一些魔力?

PyImport_ImportModule is part of the C ABI, which means it can have performance penalty: https://docs.python.org/3/c-api/stable.html#stable-abi-list PyImport_ImportModule 是 C ABI 的一部分,这意味着它可能会受到性能损失: https://docs.python.org/3/chtml-list/stable-abi-list/stable-abi。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM