.net - C++ ~ 1M 在 unordered_map 中使用字符串键查找比 .NET 代码

我有一个性能测试函数的 .NET 和 C++ 实现,它使用来自 6838 个键的池中的字符串键在字典中进行 854,750 次查找。我编写了这些函数来调查实际应用程序中的性能瓶颈。

.NET 实现是用 F# 编写的,使用 Dictionary 并针对 .NET 4.0 编译

C++ 实现使用 std::unordered_map 并在 Release模式下使用 VS2010 构建。

在我的机器上,.NET 代码平均运行 240 毫秒,C++ 代码平均运行 630 毫秒。能否请您帮助我了解造成速度如此巨大差异的原因是什么?

如果我在 C++ 实现中缩短 key 长度并使用“key_”前缀而不是“key_prefix_”,它将在 140 毫秒内运行。

我尝试的另一个技巧是将 std::string 替换为自定义的不可变字符串实现,该实现具有指向源的 const char* 指针和一次性计算的哈希值。使用此字符串可以将 C++ 实现的性能降低到 190 毫秒。

C++ 代码:

struct SomeData
{
public:
    float Value;
};

typedef std::string KeyString;
typedef std::unordered_map<KeyString, SomeData> DictionaryT;

const int MaxNumberOfRuns = 125;
const int MaxNumberOfKeys = 6838;

DictionaryT dictionary;
dictionary.rehash(MaxNumberOfKeys);

auto timer = Stopwatch::StartNew();

int lookupCount = 0;

char keyBuffer[100] = "key_prefix_";
size_t keyPrefixLen = std::strlen(keyBuffer);

/// run MaxNumberOfRuns * MaxNumberOfKeys iterations
for(int runId = 0; runId < MaxNumberOfRuns; runId++)
{
    for(int keyId = 0; keyId < MaxNumberOfKeys; keyId++)
    {
        /// get a new key from the pool of MaxNumberOfKeys keys           
        int randomKeySuffix = (std::rand() % MaxNumberOfKeys);
        ::itoa(randomKeySuffix, keyBuffer + keyPrefixLen, 10);

        KeyString key = keyBuffer;

        /// lookup key in the dictionary         
        auto dataIter = dictionary.find(key);
        SomeData* data;

        if(dataIter != dictionary.end())
        {
            /// get existing value           
            data = &dataIter->second;
        }
        else
        {
            /// add a new value
            data = &dictionary.insert(dataIter, DictionaryT::value_type(key, SomeData()))->second;
        }

        /// update corresponding value in the dictionary
        data->Value += keyId * runId;
        lookupCount++;
    }
}

timer.Stop();
std::cout << "Time: " << timer.GetElapsedMilleseconds() << " ms" << std::endl;
std::cout << "Lookup count: " << lookupCount << std::endl;

打印:

时间:636 毫秒
查询次数:854750

F# 代码

open System
open System.Diagnostics
open System.Collections.Generic

type SomeData =
    struct
        val mutable Value : float
    end

let dictionary = new Dictionary<string, SomeData>()
let randomGen = new Random()

let MaxNumberOfRuns = 125
let MaxNumberOfKeys = 6838

let timer = Stopwatch.StartNew()

let mutable lookupCount = 0

/// run MaxNumberOfRuns * MaxNumberOfKeys iterations
for runId in 1 .. MaxNumberOfRuns do
    for keyId in 1 .. MaxNumberOfKeys do

        /// get a new key from the pool of MaxNumberOfKeys keys
        let randomKeySuffix = randomGen.Next(0, MaxNumberOfKeys).ToString()        
        let key = "key_prefix_" + randomKeySuffix

        /// lookup key in the dictionary
        let mutable found, someData = dictionary.TryGetValue (key)
        if not(found) then
            /// add a new value
            someData <- new SomeData()
            dictionary.[key] <- someData

        /// update corresponding value in the dictionary
        someData.Value <- someData.Value + float(keyId) * float(runId)

        lookupCount <- lookupCount + 1

timer.Stop()

printfn "Time: %d ms" timer.ElapsedMilliseconds
printfn "Lookup count: %d" lookupCount

打印:

时间:245 毫秒
查询次数:854750

最佳答案

Visual Studio 2010 对 std::string 使用高性能哈希函数,而不是准确的哈希函数。基本上,如果键字符串大于 10 个字符,散列函数将停止使用每个字符进行散列,并且步长大于 1

size_t operator()(const _Kty& _Keyval) const
    {   // hash _Keyval to size_t value by pseudorandomizing transform
    size_t _Val = 2166136261U;
    size_t _First = 0;
    size_t _Last = _Keyval.size();
    size_t _Stride = 1 + _Last / 10;

    for(; _First < _Last; _First += _Stride)
        _Val = 16777619U * _Val ^ (size_t)_Keyval[_First];
    return (_Val);
    }
  • size() >= 10 - 在第一个字符之后使用第二个字符
  • size() >= 20 - 在第一个字符之后每隔三个字符使用一次
  • ...

因此,冲突发生得更频繁,这当然会减慢代码速度。尝试 C++ 版本的自定义哈希函数。

关于.net - C++ ~ 1M 在 unordered_map 中使用字符串键查找比 .NET 代码慢得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8372579/

相关文章:

c++ - 在创建派生类对象时将参数传递给基类构造函数

c++ - 如何将 long 移位超过 32 位?

c++ - `typedef typename Foo::Bar Bar'的模板声明

c++ - 为什么在传递给另一个对象时调用 const 对象上的 std::move 会调用复制构造

c++ - c.中EOF的ascii值是多少?

c++ - C++ 类接口(interface)类的析构函数

c++ - .o、.a 和 .so 文件有什么区别?

c++ - 为什么会这样编译?期待 "cannot assign a constant to a n

c++ - 动态数组上基于范围的for循环?

c++ - 我可以使用基于范围的 for 循环轻松迭代 map 的值吗?