Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ STL
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 24-Apr-13 4:19am
linako235
Edited 24-Apr-13 9:44am
v5
Comments
linako at 24-Apr-13 14:09pm
   
Hm, now it is deleting my hash_set forever when I'm exiting the method in which this hash_set was created... (in debug mode)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

STL is very slow while debugging in Visual Studio due to the iterator debugging support.
You can speed this up dramatically by setting
#define _HAS_ITERATOR_DEBUGGING 0
 
Quelle: http://msdn.microsoft.com/en-us/library/aa985939(v=vs.80).aspx[^]
  Permalink  
v4
Comments
linako at 24-Apr-13 10:32am
   
Thank you, it improved speed dramatically!
Philippe Mori at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
du[DE] at 25-Apr-13 0:53am
   
A good point, +5! :)
linako at 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 at 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 at 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 at 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.
linako at 25-Apr-13 10:09am
   
You know - set actually gave much better performance - I was skeptical about the set before, because I though that searching in set will me much slower than in hash_set, but it actually wasn't. Thank you!
Philippe Mori at 25-Apr-13 10:22am
   
Theorically, an hast_set should be faster (for search) provided that you don't have too much collisions, that computing the hash is fast and comparing object is slow.
I have never used an hash_set in C++.
linako at 25-Apr-13 10:33am
   
Actually, my data set didn't have any collisions. I didn't do any bench-marking to compare search in set & hash_set, in theory set will give O(log n) vs O(1) for hash_set, but I didn't notice much difference. But set performed extremely good for inserting data vs hash_set, and set was deleted in a whim compared to hours!!! for hash_set. May be because hash_set is not from standard library, it is not a good container to use... (this all relates only to debug mode)

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

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100