Странное выполнение С++ кода
Может кто сталкивался или может обьяснить в чем может быть дело.
Есть две версии одного и тогоже кода.
Для замеров оба куска кода 10М итераций.
bool HArrayFixRAM::insert(uint* key, uint value)
{
if (pLastContentCell + RowLen > pLastContentCellOnLastPage)
{
ContentPage* pLastContentPage = new ContentPage();
pContentPages[ContentPagesCount++] = pLastContentPage;
if (ContentPagesCount == ContentPagesSize)
{
reallocateContentPages();
}
pLastContentCell = pLastContentPage->pContent;
pLastContentCellOnLastPage = pLastContentCell + MAX_SHORT;
}
//insert value ============
uint keyOffset = 0;
uint headerOffset = key[0] >> HeaderBits;
ContentCell* pContentCell = pHeader[headerOffset];
if (!pContentCell)
{
pHeader[headerOffset] = pLastContentCell;
pLastContentCell->Type = (ONLY_CONTENT_TYPE + KeyLen);
//fill key
for (; keyOffset < KeyLen; keyOffset++, pLastContentCell++)
{
pLastContentCell->Value = key[keyOffset];
}
pLastContentCell->Value = value;
pLastContentCell++;
return true;
}
return true;
Эта версия выполняется около 850 мсек.
Ровно такая же версия, но на один блок выполнения длинее
(if закомментирован в середине).
bool HArrayFixRAM::insert(uint* key, uint value)
{
if (pLastContentCell + RowLen > pLastContentCellOnLastPage)
{
ContentPage* pLastContentPage = new ContentPage();
pContentPages[ContentPagesCount++] = pLastContentPage;
if (ContentPagesCount == ContentPagesSize)
{
reallocateContentPages();
}
pLastContentCell = pLastContentPage->pContent;
pLastContentCellOnLastPage = pLastContentCell + MAX_SHORT;
}
//insert value ============
uint keyOffset = 0;
uint headerOffset = key[0] >> HeaderBits;
ContentCell* pContentCell = pHeader[headerOffset];
//COMMENTED HERE, BLOCK WILL BE EXECUTED ALWAYS
//if (!pContentCell)
{
pHeader[headerOffset] = pLastContentCell;
pLastContentCell->Type = (ONLY_CONTENT_TYPE + KeyLen);
//fill key
for (; keyOffset < KeyLen; keyOffset++, pLastContentCell++)
{
pLastContentCell->Value = key[keyOffset];
}
pLastContentCell->Value = value;
pLastContentCell++;
return true;
}
return true;
Вдруг работает в два раза быстрее, всего 405 мсек о_О.
По логике ведь все должно быть наоборот. Второй кусок кода на целый блок комманд выполнения длинее.
Чую както все это связано с ковеером проца, кешами L1/L2, но пока не представляю как это все можно связать. Может у кого есть простое обьяснение, почему так произошло.
55 коментарів
Додати коментар Підписатись на коментаріВідписатись від коментарів