Use Visual Studio C ++'s default hash_map with caution -- reprint

Source: Internet
Author: User

I wrote a module client and shared it with the server. In order to speed up the query, I used hash_map. Today, a brother Lu tested that replacing hash_map with map is faster and a little dizzy. Write a paragraph by yourselfCodeUse Visual Studio C ++ in Windows to test hashmap.

First of all, hashmap has not yet entered the C ++ specification, but most vendors have implemented this container. There are two types of hashmap:

L default hash_map in Visual Studio 2003,

L hash_map of stlport

Of course, I have used Visual Studio's default hashmap fraternity. I know that it is different from the STL Implementation of SGI, including the template declaration method. So pay attention to it when using it. Here we will not bother with this problem. At the same time, please note that the tested version is 2003 and the debug version is used.

The test code is as follows. If you think it is boring, you can skip this section and view the result directly. Some codes in the Code use the ace code to test the time difference:

# Include <stdio. h>

# Include <iostream>

# Include <map>

# Include

# Include <ACE/OS. h>

# Include <ACE/time_value.h>

 

 

Void test_hash_map ()

 

{

 

Ace_time_value tvstart (0 );

Ace_time_value tvend (0 );

Ace_time_value tvpasstime (0 );

Tvstart = ace_ OS: gettimeofday ();

 

Hash_map <size_t, int> int_hash_map;

// Test 0.1 million times

Const size_t test_number = 10*10000;

// Pay attention to this line of code. The default STL of vs. Net does not have this function, and the stlport implementation has this function.

Int_hash_map.resize (test_number );

 

// Insert a group of data in sequence

For (size_t I = 0; I <test_number; ++ I)

{

Int_hash_map [I] = 0;

}

 

// Query 0.2 million times, which can be queried normally, but cannot be found in half

For (size_t I = 0; I <2 * test_number; ++ I)

{

Int_hash_map.find (I );

}

// Get the time difference in milliseconds

Tvend = ace_ OS: gettimeofday ();

Tvpasstime = tvend-tvstart;

Cout <"test_hash_map gettimeofday:" <tvpasstime. msec () <"<Endl;

};

 

Void test_map ()

 

{

 

Ace_time_value tvstart (0 );

Ace_time_value tvend (0 );

Ace_time_value tvpasstime (0 );

Tvstart = ace_ OS: gettimeofday ();

 

Map <size_t, int> int_map;

// Test 0.1 million pieces of data

Const size_t test_number = 10*10000;

For (size_t I = 0; I <test_number; ++ I)

{

Int_map [I] = 0;

}

 

For (size_t I = 0; I <2 * test_number; ++ I)

 

{

Int_map.find (I );

}

// Get the time difference in milliseconds

Tvend = ace_ OS: gettimeofday ();

Tvpasstime = tvend-tvstart;

Cout <"test_map gettimeofday:" <tvpasstime. msec () <"<Endl;

 

};

 

Int main (INT argc, char * argv [])

 

{

For (Int J = 0; j <10; ++ J)

{

Test_hash_map ();

Test_map ();

}

Return 0;

}

The default STL test result of Visual Studio is that, surprisingly, the speed of hash_map is not as high as that of map (tested on a machine of my colleague vs2005, MAP is faster than hash_map). It saves space and only writes two groups of test results. Other test results are slightly different.

Test_hash_map gettimeofday: 3093

Test_map gettimeofday: 3484

Test_hash_map gettimeofday: 3250

Test_map gettimeofday: 3531

The test result of using stlport is as follows: hash_map is more than twice faster than ms, and map is faster than Ms.

Test_hash_map gettimeofday: 1312

Test_map gettimeofday: 2359

Test_hash_map gettimeofday: 1312

Test_map gettimeofday: 2375

Since the hash_map Implementation of Ms does not have the resize function, I tested the stlport implementation and first used the resize function. The result is as follows, the speed can be greatly improved.

Test_hash_map gettimeofday: 1015

Test_map gettimeofday: 2343

Test_hash_map gettimeofday: 1031

Test_map gettimeofday: 2375

 

I am familiar with the implementation of stlport hash_map. It should be the array of the hash factor plus the list to store data. The implementation of MS Visual Studio is similar to that of MS Visual Studio. I cannot tell the specific reason, and I do not want to spend much energy to find out the crux of the problem, let's just look at the surface phenomenon.

Therefore, when using hash_map in Visual Studio, be careful,

L first, this hash_map implementation is not very fast,

L second, Microsoft's implementation is not authentic. Basically, it is certain that the implementation of SGI will be the default standard. If you are interested, you can look at boost's unordered_map. In the future, hash_map should be like this.

L Third, there is no resize function, so it is almost certain that this implementation of large capacity performance should be very bad. My colleague tested the above in 2005ProgramAfter the number of tests was changed to 1 million, he said that he had no patience to wait for the results. The stlport test result of 2003 is over 10 s. I am sure why, I suggest you take a look at the implementation of hash_map.

In addition, inmore looked at my test results and said that it seems that the traversal of the iterator implemented by MS is faster. I tested it and found that it is true that Microsoft engineers are stunned, the iterator for optimizing hash_map is traversed, but it is somewhat doubtful that this is related to the MS implementation.

Microsoft's own compiler, the implementation of things is an order of magnitude slower than others, Ms engineers should be ashamed of one. The test is limited to 2003 and 2005, and does not involve 2008, but I don't have much hope. Of course, I would not advise you to replace everything with the implementation of stlport. Why? If you use a large number of third-party libraries, the cost is too high. Unless you are like me, it is a different type of server code debugging in windows.

 

This article is from the csdn blog. For more information, see the source:Http://blog.csdn.net/fullsail/archive/2009/06/01/4230312.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.