gpt4 book ai didi

c++ - 像 C# HashSet 和 Dictionary 这样的快速 C++ 容器?

转载 作者:可可西里 更新时间:2023-11-01 18:36:29 25 4
gpt4 key购买 nike

我在 C# 中经常使用 HashSet 和 Dictionary,发现它们非常快......

我尝试过使用 std::map 和 std::hash_map 并且发现它们比较起来非常慢。这听起来像预期的行为吗?我在使用 std::hash_map 时是否可能做错了什么?

或者,是否有更好的 C++ 哈希容器?

我正在对 int32 进行哈希处理,通常大约有 100,000 个。

更新:我用 C# 和 C++ 创建了一个重现。它运行两次试验,在 C# 中分别花费 19 毫秒和 13 毫秒,在 C++ 中花费大约 11,000 毫秒。我的 C++ 代码一定有问题 :)

(两者都作为发布版本运行,都是控制台应用程序)

C# 输出:

Found 511 values in the intersection, in 19 ms
Found 508 values in the intersection, in 13 ms

C++ 输出:

Found 308 values in the intersection, in 11764.7ms
Found 316 values in the intersection, in 11742.8ms

C++ 输出(使用 stdext::hash_map 而不是 std::map)

Found 300 values in the intersection, in 383.552ms
Found 306 values in the intersection, in 2277.02ms

C++ 输出(使用 stdext::hash_map,x64 版本构建)

Found 292 values in the intersection, in 1037.67ms
Found 302 values in the intersection, in 3663.71ms

注意事项:

  • Set2 在 C++ 中没有像我想要的那样填充,我期望它与 Set1 有 50% 的交集(就像在 C# 中一样),但出于某种原因我不得不将我的随机数乘以 10让它们部分不相交

C#:

    static void Main(string[] args)
{
int start = DateTime.Now.Millisecond;
int intersectionSize = runIntersectionTest();
int duration = DateTime.Now.Millisecond - start;

Console.WriteLine(String.Format("Found {0} values in the intersection, in {1} ms", intersectionSize, duration));

start = DateTime.Now.Millisecond;
intersectionSize = runIntersectionTest();
duration = DateTime.Now.Millisecond - start;

Console.WriteLine(String.Format("Found {0} values in the intersection, in {1} ms", intersectionSize, duration));

Console.ReadKey();
}

static int runIntersectionTest()
{
Random random = new Random(DateTime.Now.Millisecond);

Dictionary<int,int> theMap = new Dictionary<int,int>();

List<int> set1 = new List<int>();
List<int> set2 = new List<int>();

// Create 100,000 values for set1
for ( int i = 0; i < 100000; i++ )
{
int value = 1000000000 + i;
set1.Add(value);
}

// Create 1,000 values for set2
for ( int i = 0; i < 1000; i++ )
{
int value = 1000000000 + (random.Next() % 200000 + 1);
set2.Add(value);
}

// Now intersect the two sets by populating the map
foreach( int value in set1 )
{
theMap[value] = 1;
}

int intersectionSize = 0;

foreach ( int value in set2 )
{
int count;
if ( theMap.TryGetValue(value, out count ) )
{
intersectionSize++;
theMap[value] = 2;
}
}

return intersectionSize;
}

C++:

int runIntersectionTest()
{
std::map<int,int> theMap;

vector<int> set1;
vector<int> set2;

// Create 100,000 values for set1
for ( int i = 0; i < 100000; i++ )
{
int value = 1000000000 + i;
set1.push_back(value);
}

// Create 1,000 values for set2
for ( int i = 0; i < 1000; i++ )
{
int random = rand() % 200000 + 1;
random *= 10;

int value = 1000000000 + random;
set2.push_back(value);
}

// Now intersect the two sets by populating the map
for ( vector<int>::iterator iterator = set1.begin(); iterator != set1.end(); iterator++ )
{
int value = *iterator;

theMap[value] = 1;
}

int intersectionSize = 0;

for ( vector<int>::iterator iterator = set2.begin(); iterator != set2.end(); iterator++ )
{
int value = *iterator;

map<int,int>::iterator foundValue = theMap.find(value);

if ( foundValue != theMap.end() )
{
theMap[value] = 2;

intersectionSize++;
}
}

return intersectionSize;

}

int _tmain(int argc, _TCHAR* argv[])
{
srand ( time(NULL) );

Timer timer;
int intersectionSize = runIntersectionTest();
timer.Stop();

cout << "Found " << intersectionSize << " values in the intersection, in " << timer.GetMilliseconds() << "ms" << endl;

timer.Reset();
intersectionSize = runIntersectionTest();
timer.Stop();

cout << "Found " << intersectionSize << " values in the intersection, in " << timer.GetMilliseconds() << "ms" << endl;

getchar();

return 0;
}

最佳答案

Hash_map 和 hash_set 是非标准的,unordered_mapunordered_set最有可能很快成为标准版本。如果没有复制器,我认为这不会走得太远。在底层,它们是相同的数据结构,因此它们应该具有相似的性能。


我在 MS Visual Studio 2008 v9.0.30729.1 下将提供的示例编译为 Visual C++ -> Win32 -> 控制台应用程序(尽管我推出了自己的 Timer 类,因为我不确定您使用的是什么)。在调试下,我得到了 1000 毫秒的时间,但在发布下编译是 50 毫秒。

#include <vector>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <windows.h>

typedef struct {
LARGE_INTEGER start;
LARGE_INTEGER stop;
} stopWatch;

class CStopWatch {

private:
stopWatch timer;
LARGE_INTEGER frequency;
double LIToSecs( LARGE_INTEGER & L);
public:
CStopWatch();
void startTimer( );
void stopTimer( );
double getElapsedTime();
};

double CStopWatch::LIToSecs( LARGE_INTEGER & L) {
return ((double)L.QuadPart /(double)frequency.QuadPart) ;
}

CStopWatch::CStopWatch(){
timer.start.QuadPart=0;
timer.stop.QuadPart=0;
QueryPerformanceFrequency( &frequency ) ;
}

void CStopWatch::startTimer( ) {
QueryPerformanceCounter(&timer.start) ;
}

void CStopWatch::stopTimer( ) {
QueryPerformanceCounter(&timer.stop) ;
}

double CStopWatch::getElapsedTime() {
LARGE_INTEGER time;
time.QuadPart = timer.stop.QuadPart - timer.start.QuadPart;
return LIToSecs( time) ;
}

using namespace std;
int runIntersectionTest()
{
std::map<int,int> theMap;

vector<int> set1;
vector<int> set2;

// Create 100,000 values for set1
for ( int i = 0; i < 100000; i++ )
{
int value = 1000000000 + i;
set1.push_back(value);
}

// Create 1,000 values for set2
for ( int i = 0; i < 1000; i++ )
{
int random = rand() % 200000 + 1;
random *= 10;

int value = 1000000000 + random;
set2.push_back(value);
}

// Now intersect the two sets by populating the map
for ( vector<int>::iterator iterator = set1.begin(); iterator != set1.end(); iterator++ )
{
int value = *iterator;

theMap[value] = 1;
}

int intersectionSize = 0;

for ( vector<int>::iterator iterator = set2.begin(); iterator != set2.end(); iterator++ )
{
int value = *iterator;

map<int,int>::iterator foundValue = theMap.find(value);

if ( foundValue != theMap.end() )
{
theMap[value] = 2;

intersectionSize++;
}
}

return intersectionSize;

}

int main(int argc, char* argv[])
{
srand ( time(NULL) );
int tests = 2;
while(tests--){
CStopWatch timer;
timer.startTimer();
int intersectionSize = runIntersectionTest();
timer.stopTimer();

cout << "Found " << intersectionSize << " values in the intersection, in " << timer.getElapsedTime() << "s\r\n";
}

getchar();

return 0;
}

(我会尝试使用 unordered_map 但我的版本没有)。我怀疑你的 C++ 设置有问题。

关于c++ - 像 C# HashSet<T> 和 Dictionary<K,V> 这样的快速 C++ 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1056366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com