-
Apr 27th, 2017, 11:57 AM
#1
Thread Starter
Lively Member
.net Dictionary equivalent in c++ (performance)
-
Apr 27th, 2017, 02:17 PM
#2
Re: .net Dictionary equivalent in c++ (performance)
Consider
Code:
#include <map>
#include <iostream>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
using namespace std;
int main()
{
map<int, bool> mib;
mib.emplace(1, false);
DWORD currentTick = GetTickCount();
for (DWORD i = 0; i < 120000; ++i)
if (mib.count(3)) {}
DWORD endTick = GetTickCount();
DWORD final = endTick - currentTick;
cout << final << endl;
}
With VS2017 in release build with full optimization on my Windows 7 system this gives
Output : 0 ms
PS On my system your c++ code in post #1 also has output of 0 ms! How is your compiler configured? Is it set to compile in release mode as opposed to debug mode and have you enabled all full optimisations?
Last edited by 2kaud; Apr 27th, 2017 at 02:25 PM.
Reason: PS
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Apr 27th, 2017, 02:37 PM
#3
Re: .net Dictionary equivalent in c++ (performance)
Note that using binary_search() requires the sequence between first and last iterator to be in sorted order. This is not automatic with vector but is with map. If vector is used then it would have to be sorted first before binary_search() is used. Note also that with map, to find an item the map .find() would be used (or .count() to find if a specific key exists) in preference to binary_search() as internally map uses a tree structure to store its data.
See http://www.cplusplus.com/reference/map/map/
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Apr 27th, 2017, 09:00 PM
#4
Thread Starter
Lively Member
Re: .net Dictionary equivalent in c++ (performance)
Hi! You're right,
I change the build mode to 'Release' and the performance is quite different. The output show 0ms now, C++ now can beat vb.net .
- I tried to increase the iteration to 12000000 , with C++ it cost 32-47ms while vb.net give 220ms. Very cool!
Another choice is to use unordered_set (hash_set) or set will also give high performance: http://stackoverflow.com/questions/5...ist-of-numbers
Thank you very much for replying to my thread and help me
Last edited by vietnamvodich; Apr 27th, 2017 at 09:57 PM.
-
Apr 27th, 2017, 09:58 PM
#5
Thread Starter
Lively Member
Re: .net Dictionary equivalent in c++ (performance)
Hi, another test indicated that the speed is seems a little different between Visual Studio 2010 and Visual Studio 2017 compiler too.
I have tested with 120.000.000 iteration on Visual Studio 2010 and Visual Studio 2017: (both full optimized and release mode). I ran the compiled EXE instead of run it from Visual Studio. This is the result:
Visual Studio 2010 : 297 ms
Visual Studio 2017 : 360 ms
It seems that older is better
Last edited by vietnamvodich; Apr 27th, 2017 at 10:03 PM.
-
Apr 28th, 2017, 01:59 AM
#6
Re: .net Dictionary equivalent in c++ (performance)
VS2010 is pre c++11 and so doesn't conform to the c++11 standard - nor the later c++14 standard - which VS2017 supports (and some of the forthcoming c++17 standard as well).
Hence there have been major changes to the compiler between that used by VS2010 and VS2017 - both to support the revised c++ standard and also internally how the compiler is structured. Unless there is a show-stopping breaking change between compilers, it is always recommended that the latest version is used.
Note that on the code in post #2 with a loop iteration of 120,000,000 on my system I still get a run time of 0 ms
Are you sure you have full optimisation enabled? Optimization: Full; Favor Size or Speed: Favor fast code; Whole Program Optimization: yes.
Yes - depending upon what you are really trying to achieve. I just posted some test code to beat VB . When dealing with large volumes of data and/or frequent inserts/deletions/look-ups then profiling is recommended to see where are the bottlenecks so that these can be adjusted as needed. Most solutions have some trade-offs and in particular scenarios some trade-offs may be better than others.
Last edited by 2kaud; Apr 28th, 2017 at 02:12 AM.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Apr 28th, 2017, 03:52 AM
#7
Thread Starter
Lively Member
Re: .net Dictionary equivalent in c++ (performance)
Hi!
Thank you for these useful information.
Yes, I've used Optimization: Full; Favor Size or Speed: Favor fast code; Whole Program Optimization: yes.
Note that on the code in post #2 with a loop iteration of 120,000,000 on my system I still get a run time of 0 ms
It's because the compiler in some case can understand that these code return an unused variable and the compiler will not compile these code. Try this code, it will show the real performance:
Code:
map<int, bool> mib;
mib.emplace(1, false);
int a = 1; //declare a variable
DWORD currentTick = GetTickCount();
for (DWORD i = 0; i < 120000000; ++i)
if (mib.count(3)) {
a = 123; //whatever , just a fake code to let the compiler understand that we are using the fake variable
}
DWORD endTick = GetTickCount();
DWORD final = endTick - currentTick;
cout << final << ":" << a << endl; // use it at the end
-
Apr 28th, 2017, 05:51 AM
#8
Re: .net Dictionary equivalent in c++ (performance)
Yep, tried that. Also ++a instead of a = 123. Still the same - 0 ms.
Also
Code:
if (!mib.count(3)) { ++a; }
Still 0 ms!
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Apr 28th, 2017, 06:56 AM
#9
Thread Starter
Lively Member
Re: .net Dictionary equivalent in c++ (performance)
That's quite weird, have you used this code at the end :
Code:
cout << final << ":" << a << endl; // use it at the end
Without that code, i got 0ms too. Or may be your computer is too fast, try to increase the iteration to 10 or 100 times or even 10000 times. If it still showing 0 ms, there must be some problem here.
-
Apr 28th, 2017, 08:08 AM
#10
Re: .net Dictionary equivalent in c++ (performance)
Yeah, when I use a in the final cout I now get a non 0 value! If a is not used after the loop the optimising compiler realises this and just eliminates the loop!
For 120,000,000 I get 967 ms for 12,000,000 I get 94 ms and for 1,200,000 I get 15 ms - but I'm using a quite old laptop.
Last edited by 2kaud; Apr 28th, 2017 at 08:14 AM.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|