14

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++;
                }
            }
        }
    }
}
9
  • 1
    Why not sort the list and use std::unique?
    – Kerrek SB
    Sep 27, 2012 at 23:37
  • 5
    One general way of doing this that would be better is to iterate through the list and store each item in a set (if it isn't already there) as you go, then convert the set back to a list. That's O(n) and it doesn't modify the list as you're traversing it. Kerrek's suggestion is good, and there are probably other ways that are better.
    – i_am_jorf
    Sep 27, 2012 at 23:39
  • 1
    Likely because of debug iterators and checked iterators being enabled by default in debug builds (plus, you know, the whole optimizations being disabled thing). Go to the links to see how to disable them, and see if that helps.
    – ildjarn
    Sep 27, 2012 at 23:39
  • I'd do it as jeffamaphone suggested. Also, IMO, changing the index of a for loop inside the loop is not good programming practice. If you want to do that make it a while loop. (Yeah I know, it works, but it causes confusion).
    – John3136
    Sep 27, 2012 at 23:41
  • 1
    Why the upvotes? This is such a basic question, and is highly like to be a duplicate of any question asking "why does debugging make things slow?"
    – Drise
    Sep 27, 2012 at 23:46

1 Answer 1

22

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.

7
  • I tried to make this work for quite some time but without success. Other libraries I use forbid changing _HAS_ITERATOR_DEBUGGING to 0
    – Pat
    Sep 27, 2012 at 23:46
  • 3
    @Pat: You can always fall back to tried and true printf() debugging in release mode. If it's good enough for Linux kernel devs, it's should be good enough for anyone! Another possible solution is to change the Release configuration for the project to compile with optimizations off and symbols on. Then debug the Release build. It'll link to non-debug libraries, but you should still be able to step through your code just fine. Set optimization back on when you're done. Sep 28, 2012 at 0:12
  • 1
    In MSVC, you can individually undefine _HAS_ITERATOR_DEBUGGING on a per project or per file basis by right clicking the troublemaker then "Properties" -> "C/C++" -> "Preprocessor" -> "Undefine Preprocessor Definitions" Oct 17, 2018 at 3:59
  • 1
    You can also isolate it to a certain bit of code with #pragma push_macro/pop_macro and #defining it as 0 in between. Oct 17, 2018 at 4:00
  • 2
    For msvc 2015 they have moved to _ITERATOR_DEBUG_LEVEL; combining both _HAS_ITERATOR_DEBUGGING and the less problematic _SECURE_SCL Oct 17, 2018 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.