Question

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.

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. The next time it takes around 200 ms. 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.

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.

Thank you for your ideas.

Was it helpful?

Solution

You're reading 12.5MB of data from the file. 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.

However, 5 seconds for reading 12.5MB of data is still a lot. 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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top