【发布时间】:2018-05-29 15:09:58
【问题描述】:
我真的不知道怎么问这个问题,但我会尽量说清楚。
我正在计时从 python 调用 C++ 函数。 C++ 函数用 cython 包装。
我目前正在计时 cython 函数的 python 调用,我用time.time() 得到了 52.9 毫秒。另一方面,我正在使用 C++ std::chrono::high_resolution_clock 库为整个 C++ 函数计时。
问题是,我在 C++ 中测量了 17.1 毫秒。
C++ 函数是这样声明的 vector<float> cppfunc(vector<float> array, int a, int b, int c); 并且是一个 A 类方法。
cython 代码只调用 C++ 类方法。该向量包含大约 320k 个元素。
我想知道这两个测量的时间是否可以这样比较? 如果可以,如何解释这种差距? 如果没有,我应该使用哪种计时工具?
Edit1:(cmets 中的链接)两个计时库对于我的用例来说都足够精确(我的拱门上的 cpp 为 10e-9,python 为 10e-6)。
Edit2:添加了简化代码来说明我的观点。使用此代码,python 调用持续时间(~210ms)是实习生 cpp 持续时间(~28ms)的 8 倍。
// example.cpp
#include "example.h"
#include <iostream>
#include <chrono>
std::vector<float> wrapped_function(std::vector<float> array)
{
auto start = std::chrono::high_resolution_clock::now();
std::vector<float> result;
for (int i = 0; i < (int) array.size(); i++) {
result.push_back(array[i] / 1.1);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> duration = end - start;
printf("Within duration: %.5f\n", duration.count());
return result;
}
// example.h
#ifndef __EXAMPLE_H_
#define __EXAMPLE_H_
#include <vector>
std::vector<float> wrapped_function(std::vector<float> array);
#endif
# example_wrapper.pxd
from libcpp.vector cimport vector
cdef extern from "example.h":
vector[float] wrapped_function(vector[float])
# example_wrapper.pyx
from example_wrapper cimport wrapped_function
def cython_wrap(array):
return wrapped_function(array)
# setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
cmdclass = {"build_ext": build_ext},
ext_modules = [
Extension(name="example_wrapper",
sources=["example_wrapper.pyx", "example.cpp"],
include_dirs=["/home/SO/"],
language="c++",
extra_compile_args=["-O3", "-Wall", "-std=c++11"]
)
]
)
# test.py
import example_wrapper
from time import time
array = [i for i in range(1000000)]
t0 = time()
result = example_wrapper.cython_wrap(array)
t1 = time()
print("Wrapped duration: {}".format(t1 - t0))
【问题讨论】:
-
您应该调查每种计时方法的精确度。时钟的粒度可达几十毫秒。
-
你的 C++ 时间是否包括大向量参数的复制?或者你只是在函数本身内计时?
-
我只是在函数内部计时,既然你这么说,它可能与数组的大小高度相关。
-
我首先认为这个问题必须是理论上的,我正在寻找一般建议,但你是对的,所以我添加了一个示例源代码。很抱歉给您带来不便。
标签: c++ python-3.x time cython wrapper