Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I need to insert approx. 5,000,000 integers to a hash_set in C++, it takes many hours to go through the loop in debug mode. In release mode it slows down the program, takes about a minute, which is not fantastic, but I can live with it. Is it normal for hash_set? Insert should be O(1)...
Posted
Updated 24-Apr-13 8:44am
v5
Comments
linako 24-Apr-13 14:09pm    
Hm, now it is deleting my hash_set<int> forever when I'm exiting the method in which this hash_set was created... (in debug mode)

STL is very slow while debugging in Visual Studio due to the iterator debugging support.
You can speed this up dramatically by setting
C++
#define _HAS_ITERATOR_DEBUGGING 0


Quelle: http://msdn.microsoft.com/en-us/library/aa985939(v=vs.80).aspx[^]
 
Share this answer
 
v4
Comments
linako 24-Apr-13 10:32am    
Thank you, it improved speed dramatically!
Philippe Mori 24-Apr-13 23:32pm    
Even without check, debugging STL in mixed-mode application is sometime much slower than debugging equivalent C# code using .NET containers. Some C++ code run much slower Under the debugger.
If you are compiling a C++/CLI (mixed-mode) application, you might want to compare the performative with .NET containers. Often they are much faster than C++ one in mixed/mode.

I don't know why but there are benchmarks on the web if you want to have an idea.

By the way debugging C++/CLI code (using STL) is sometime much slower that debugging equivalent .NET code. In a way it is similar to the fact that compiling C# code is much faster that compiling C++ particulary when template are used a lot (STL or even worst some boost code).

When I start my application a few years ago, I was using a lot of C++ code as I'd like STL but with years, I tend to do more and more C# code to avoid problems that should not even exists...

This is even worst as debugging C++ code is not as friendly because the debugger is not always displaying relevant information when inspecting variables.
 
Share this answer
 
Comments
Leo Chapiro 25-Apr-13 0:53am    
A good point, +5! :)
linako 25-Apr-13 8:31am    
No, my application is pure C++, but I do use Visual Studio 2010 for development.
Is there a way not to debug part of the code in the debug mode? - as I don't really want to debug STL code at this point, but application has to pass through it before it reaches the piece of code I need to debug.
Philippe Mori 25-Apr-13 9:09am    
Usually, you want to set breakpoints after the code you don't want to debug. When you don't know, then you do it the long way the first time and add breakpoints for future debugging. Conditional breapoints might be useful too. Although they migh cause a slowdown, it is still much faster than stepping code by hand.

Another trick is to set breakpoint a few line below if you have loops or uses "Run to cursor" or "Step out" to avoid stepping more code than necessary.

Sometime you might want to "expand" some if/else conditions (or add extra conditions) while debugging so that you can put a breakpoint that would break exactly where you want. That kind of tricks might be really usefull if you have big loops and you need to make a lot of iteration before getting to the problematic case.
linako 25-Apr-13 9:16am    
What I meant is to set something like NODEBUG around the STL piece of code, so it somehow treats this piece of code in release mode, I'm not stepping through 5,000,000 lines of code for sure:), as du[DE] suggested I'm using #define _HAS_ITERATOR_DEBUGGING 0 and it speeds up the STL loop I have (in debug mode). But then, at the method's exit it still deletes hash_set forever (many hours!) in debug mode.
Philippe Mori 25-Apr-13 9:44am    
Maybe an hash_set is not the best container in your case. Have you tried a set or even a vector that is manually sorted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900