简体   繁体   English

为什么在C ++中复制无序映射比较慢?

[英]Why is copying an unordered map slow in C++?

I'm writing code on a ARM cortex A9 for a real time robotics application. 我正在ARM Cortex A9上为实时机器人应用程序编写代码。 I'm experiencing some timing issues with my interrupts. 我的中断遇到一些计时问题。 I am copying a global variable into the interrupt which takes 20us on an interrupt at 20kHz. 我正在将全局变量复制到中断中,该中断在20kHz的中断时需要20us。 That leaves me about 40us for the idle.. Just copying takes 20us! 这使我大约有40us的空闲时间。.仅复制就需要20us!

So my question is why copying an unordered map into another unordered map take so much time? 所以我的问题是为什么将一个无序图复制到另一个无序图会花费很多时间?

class Supervisor {
private:
    DispatchCommunication dispatchComm;
    DispatchLogic dispatchLogic;

    std::unordered_map<std::string, uint32_t> fetchedData;
public:
    void initialize(void);
    void communication(void);
    void logic(void);
};

inline void Supervisor::initialize(void) {
    dispatchComm.initialize();

    dispatchLogic.initialize();    
}

/**
 * Get all sensor data
 */
inline void Supervisor::communication(void) {

    // Create a map of the data we want to request from the communication dispatcher
    std::vector<std::string> requestMap = { "jointAngle", "motorAngle", "piggyback" };

    // Fetch the requested data
    fetchedData = dispatchComm.getDataVector(requestMap);


}

/**
 * Run all the logic
 */
inline void Supervisor::logic(void) {

    std::unordered_map<std::string, uint32_t> IRQFetchedData;

    // This part takes a whopping 20us!
    IRQFetchedData = fetchedData;

}

In your case, it's not just the copying of the map. 在您的情况下,不仅是地图的复制。 It's the copying of the std::string s within the map. 它是映射中std::string的复制。 That means (potentially) lots of memory allocations. 这意味着(可能)意味着大量的内存分配。 And even without those allocations (small-string optimization), it's still lots of copying of string data. 即使没有这些分配(小字符串优化),它仍然会大量复制字符串数据。

And that's all on top of the copying needed for the map itself, which involves its own series of allocations, based on bucket sizes, the various heap implementation strategies, etc. 这就是映射本身所需的全部复制操作,它涉及基于存储桶大小,各种堆实现策略等的自身分配系列。

If fast copying is something you need, then perhaps a sorted vector would be more appropriate, rather than an unordered_map . 如果您需要快速复制,那么排序vector可能比unordered_map更合适。 That won't help the string copying issue, but it can help the cost of copying the map itself. 那不会解决string复制的问题,但是可以帮助复制地图本身。 You can even speed that up by using a fixed-length string class, which could be trivially copyable. 您甚至可以使用固定长度的字符串类(可以轻松复制)来加快速度。 That could make copying the entire vector a single memory allocation followed by a single memcpy . 这样可以将整个vector复制到单个内存分配中,然后再复制一个memcpy

Of course, it is (theoretically) slower to access a value from a sorted vector than an unordered_map . 当然,从理论上讲,从排序的vector访问值比从unordered_map存取要慢(理论上)。 But that's the give-and-take when it comes to performance. 但这就是性能方面的付出和付出。 Fast copying or fast access; 快速复制或快速访问; which matters to you? 对您来说重要吗? Though even fast access is theoretical, primarily mattering when such maps are large . 尽管从理论上讲,即使是快速访问也是如此,但是当此类地图很大时,最重要的是

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

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