简体   繁体   English

映射文件的C ++内存复制太慢

[英]C++ memcopy from mapped file too slow

I would like to ask if anybody sees a bottle bottleneck in my code or any way to optimize it. 我想问问是否有人在我的代码中看到瓶颈或以任何方式对其进行优化。 I am thinking about if my code has a fault somewhere or if I need to choose a completely new approach. 我正在考虑我的代码是否在某处出现故障,或者是否需要选择一种全新的方法。

I have memory-mapped a file, and I need to read doubles from this memory-mapped file. 我已经有一个内存映射的文件,并且我需要从这个内存映射的文件读取双打。 I need to do this around 100.000 times as fast as possible. 我需要尽可能快地执行100.000次。

I was expecting that it would be quite fast in Release mode, but that is not the case. 我原以为在发布模式下会很快,但事实并非如此。 The first time I do it, it takes over 5 seconds. 我第一次这样做需要5秒钟以上。 The next time it takes around 200 ms. 下次大约需要200毫秒。 This is a bit faster (I guess it has to do with the way Windows handles a memory-mapped file), but it is still too slow. 这有点快(我想这与Windows处理内存映射文件的方式有关),但它仍然太慢。

void clsMapping::FeedJoinFeaturesFromMap(vector<double> &uJoinFeatures,int uHPIndex)
{
    int iBytePos=this->Content()[uHPIndex];
    int iByteCount=16*sizeof(double);

    uJoinFeatures.resize(16);
    memcpy(&uJoinFeatures[0], &((char*)(m_pVoiceData))[iBytePos],iByteCount);
}

Does anybody see a way to improve my code? 有人看到一种改进我的代码的方法吗? I hardcoded the iByteCountCount, but that did not really change anything. 我对iByteCountCount进行了硬编码,但这并没有真正改变任何东西。

Thank you for your ideas. 感谢您的想法。

You're reading 12.5MB of data from the file. 您正在从文件中读取12.5MB的数据。 That's not so much, but it's still not trivial. 虽然不算什么,但是仍然不简单。

The difference between your first and second run is probably due to file caching - the second time you want to read the file, the data is already in memory so less I/O is required. 第一次运行与第二次运行之间的差异可能是由于文件缓存-第二次您要读取文件时,数据已经在内存中,因此需要的I / O更少。

However, 5 seconds for reading 12.5MB of data is still a lot. 但是,读取12.5MB数据需要5秒。 The only reason I can find for this is that your doubles are scattered all over the file, requiring Windows read a lot more than 12.5MB to memory. 我能找到的唯一原因是,双打散布在整个文件中,要求Windows读取多于12.5MB的内存。

You can avoid memory mapping altogether. 您可以完全避免内存映射。 If the data is stored in order in the file (not consecutive, but in order - you can read the data without seeking back), you can try avoiding the memory mapped file altogether, and just seek your way to the right place. 如果数据按顺序存储在文件中(不是连续的,而是按顺序存储-您可以读取数据而无需查找),则可以尝试完全避开内存映射文件,而只是寻找正确的位置。

I doubt this will help much. 我怀疑这会不会有帮助。 Other things you can do is reorder your file, if it's at all possible, or place it on an SSD. 您可以做的其他事情是对文件重新排序(如果可能的话),或者将其放在SSD上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM