Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

c++ - Why is this code 100 times slower in debug?

I'm using MSVC 2010.

I'm trying to remove duplicate (without keeping any of them) from a list

Why is this code 100 times slower in debug mode?

Is there any other way to remove all objects that are equivalent and make it faster in debug mode?

It is to the point I can't use debug at the moment. It take minutes to process while few seconds in release.

void SomeFunction()
{
    std::list<Something> list;
    std::list<Something>::iterator it1;
    std::list<Something>::iterator it2;

    for (it1 = list.begin(); it1 != list.end(); it1++)
    {
        for (it2 = list.begin(); it2!=list.end(); it2++)
        {
            if (it1->SomeValue() == it2->SomeValue())
            {
                if (it1 != it2)
                {
                    list.erase(it1);
                    list.erase(it2);

                    it2 = list.begin();
                    it1 = it2++;
                }
            }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In general, STL is very slow while debugging in Visual Studio due to the iterator debugging support. You can speed this up dramatically by setting _HAS_ITERATOR_DEBUGGING to 0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...