Click here to Skip to main content
13,048,584 members (91,757 online)
Rate this:
Please Sign up or sign in to 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 24-Apr-13 3:19am
Updated 24-Apr-13 8:44am
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)
Rate this: bad
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

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.
Rate this: bad
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.
du[DE] 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.
linako 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 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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2017
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