Replies: 2 comments
-
I think you used dict comprehensions {w:i for w,i in c_dict.items()} in python dict, it maybe alloc the memory for one time, but your c dict used for loop for i in range(100000): c_dict[str(i)] = i so that it maybe alloc the memory for many times. Try to use for loop to fill your py_dict . |
Beta Was this translation helpful? Give feedback.
-
@2milesaway There is overhead in the module as dictionary is currently performing easy task of just assigning stuff over and over again. You want to compete with simple type that is already efficient inside Python. Remember that Python->pybind->Python conversion in this case would always come at some cost -- that means transferring strings and integers (which in Python are more complicated than cpp's int32_t or int64_t) is not exactly the place you would perform such optimisation. "Don't fix what is already working" would be a great quote here even when optimisation is mentioned. However, now we know what is a bootle neck here. Is there a way to overcome this issue? In fact there is a way if we know the routine. Let's define such function in our bindings. You need to know that dicts are simply classes under the magic auto my_map = py::bind_map<std::unordered_map<std::string, int>>(module, "MapStr2Int");
my_map.def("super_function", [](std::unordered_map<std::string, int> &self, py::list &arr)
{
std::vector<int> res;
for (auto it = arr.begin(); it != arr.end(); it++)
{
res.push_back(self[it->cast<std::string>()]);
}
return res;
}); Now it is testing time:
It is clear that moving work to pure cpp is getting us somewhere. It depends on you if you want to build on top of it. But this is just a simple example how-to start. Additionally @Leopold80 it does not matter how dict is filled in this case. It is outside of scope for time measurements. Other than that this is simple type of int which is getting copied. EDIT: just changed |
Beta Was this translation helpful? Give feedback.
-
Hi folks, I exposed a c++
unordered_map<string, int>
to python, and it turned out this map is 10x slower than python'sdict
.See code below.
On MacOS, compile it with this cmd:
Finally, compare with python
dict
in ipython:As seen,
c_dict
is much much slower than python version.Why and how to improve?
Beta Was this translation helpful? Give feedback.
All reactions