简体   繁体   中英

Qt - std::unordered_map - destruction time

I have a class with a singleton and a member std::unordered_map which has an unsigned int as key and a structure as value.

The problem occurs, when adding a large amount of elements to it.
In VS this task takes around 2 seconds. In QT it takes up to 15 seconds.

Destruction of the container's elements (end of program or in destructor of singleton class) takes in VS not even half a second. In QT this task takes a few minutes.

If anyone has an idea what the problem could be, please leave an answer or comment.

Note: The function Fill can be called a few times during run-time. It's not that great if you have to wait a few minutes to clear a container... most of the time, it seems to spend inside of _Iterator_base12::_Orphan_me

Here the class/code to reproduce (without main):

#include <unordered_map>

int RandomNumber(int min, int max)
{
    return (rand() % ((max + 1) - (min))) + (min);
}

#define MAX_OTHER_DATA 5

struct OtherData
{
    int Charges;
};

struct DataHolder
{
    unsigned int ID;
    std::string Name; //std::string / QString
    int MaxCount;
    int Stack;
    unsigned int Duration;
    OtherData other[MAX_OTHER_DATA];
    unsigned int MaxDur;
};

class Worker
{
protected:
    Worker() {}
public:
    ~Worker(void)
    {
        v.clear();
    }

    static Worker &instance(void)
    {
        static Worker work;
        return work;
    }

    void Fill()
    {
        v.clear();
        const std::size_t size = 38618; //current size - data fetched from a database
        for (unsigned int i = 0U; i < size; ++i)
        {
            DataHolder it;
            it.ID = RandomNumber(1, 60000);
            it.Name = ""; //Usually populated
            it.MaxCount = RandomNumber(0, 10);
            it.Stack = RandomNumber(1, 10000);
            it.Duration = RandomNumber(30, 250);
            it.MaxDur = RandomNumber(0, 200);
            v.insert(std::make_pair(i, it));
        }
    }
private:
    std::unordered_map<unsigned int, DataHolder> v;
};
#define sWorker Worker::instance()

The main function needs only one line sWorker.Fill(); .

So after investing more time into Google and a few websites I found this, which solved my problem when compiling in debug mode. (Release is just fine)

I've added this to the .pro file in Qt creator.

DEFINES += _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH
DEFINES += _HAS_ITERATOR_DEBUGGING=0

Note: Good for a little performance in debug mode, bad for secure checks. For me it's fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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