繁体   English   中英

pybind11 c++ unordered_map 比 python 字典慢 10 倍?

[英]pybind11 c++ unordered_map 10x slower than python dict?

我将 c++ unordered_map<string, int>暴露给 python,结果发现这个 map 比 python 的dict慢 10 倍。

请参阅下面的代码。

// map.cpp file
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <string>
#include <unordered_map>

namespace py = pybind11;

PYBIND11_MAKE_OPAQUE(std::unordered_map<std::string, int>);

PYBIND11_MODULE(map, m) {
    // map
    py::bind_map<std::unordered_map<std::string, int>>(m, "MapStr2Int");
}

在 MacOS 上,用这个 cmd 编译它:

c++ -O3 -std=c++14 -shared -fPIC -Wl,-undefined,dynamic_lookup $(python3 -m pybind11 --includes) map.cpp -o map$(python3-config --extension-suffix)

最后,与 ipython 中的 python dict进行比较:

In [20]: import map

In [21]: c_dict = map.MapStr2Int()

In [22]: for i in range(100000):
    ...:     c_dict[str(i)] = i
    ...:

In [23]: py_dict = {w:i for w,i in c_dict.items()}

In [24]: arr = [str(i) for i in np.random.randint(0,100000, 100)]

In [25]: %timeit [c_dict[w] for w in arr]
59 µs ± 2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [26]: %timeit [py_dict[w] for w in arr]
6.58 µs ± 87.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

如图所示, c_dict比 python 版本py_dict慢得多。

为什么以及如何改进?

您正在比较一个本机字典实现(来自 Python 标准库的那个)和一个 pybind 包装的实现。 我敢打赌,直接使用std::unordered_map的 C++ 程序肯定比用 Python 编写并使用dict的同等程序快。

但这不是你在这里所做的。 Instead of that, you ask pybind to generate a wrapper that will convert Python types into C++ ones, call the C++ standard library class methods and then convert back the result into a Python type. 这些转换可能需要分配和解除分配,并且确实需要一些时间。 此外, pybind是一个非常聪明(因此很复杂)的工具。 您不能期望它生成的代码与使用 Python API 的直接调用一样优化。

Unless you intend to use a specially optimized algorithm for the hashing function, you will not be able to write a C or C++ code that will be faster than the standard library, because the builtin types are already coded in C language. 如果你模仿它并直接使用Python/C API ,最多应该可以和标准库一样快。

暂无
暂无

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

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