dimanche 10 mai 2015

MFC 64 bit code is slower than 32 bit on Windows Server 2008 R2

I run the following MFC code:

CArray<CString> l_Arr;
for (int i = 0; i < 2000000; ++i)
{
  CString l_sStr;
  l_sStr.Format("%d", i);
  l_Arr.Add(l_sStr);
}

If I build 64 bit version the code runs about 2 times slower than 32 bit. I tried both Debug and Release versions. The times are the following:

Debug 64 bit: 15085 ms
Debug 32 bit: 8128 ms
Release 64 bit: 8237 ms
Release 32 bit: 4695 ms

My configuration:

Microsoft Visual Studio 2005
Version 8.0.50727.4039 (QFE.050727-4000)
Windows Server 2008 R2 Standard 64 bit
Processor: Intel(R) Xeon(R) E5645 @ 2.40GHz
Memory: 16.0 GB

I understand that this code can be optimized. I do not understand why there is such a difference.

@BarmakShemirani, I tried with vector as you suggested, you are right, the results are completely different. It looks like 64 bit code is even faster. Here are the results:

std::vector<CString> l_Arr; 
for (int i = 0; i < 2000000; ++i) 
{ 
  CString l_sStr; 
  l_sStr.Format("%d", i); 
  l_Arr.push_back(l_sStr); 
} 

Debug 64 bit: 3563 ms
Debug 32 bit: 4562 ms
Release 64 bit: 1140 ms
Release 32 bit: 1563 ms

Here is the optimized version with CArray:

CArray<CString> l_Arr;
static const int K_CNT = 2000000;
l_Arr.SetSize(K_CNT);
for (int i = 0; i < K_CNT; ++i)
{
  CString l_sStr;
  l_sStr.Format("%d", i);
  l_Arr[i] = l_sStr;
}

Debug 64 bit: 2625 ms
Debug 32 bit: 2625 ms
Release 64 bit: 1015 ms
Release 32 bit: 1438 ms

Is CArray growing mechanism causes the code to run slower in 64 bit?

I looked into CArray growing code, essentially it allocates a new array and copies to it the old content every 1024 added elements. So I just simulated the allocation code with the following results:

static const int K_CNT = 2000000;
for (int i = 0; i < K_CNT / 1024; ++i)
{
  int l_nSize = (i + 1) * 1024 * sizeof(CString);
  BYTE* l_pData = new BYTE[l_nSize];
  memset(l_pData, 0, l_nSize);
  delete[] l_pData;
}

Debug 64 bit: 10483 ms
Debug 32 bit: 4696 ms
Release 64 bit: 5803 ms
Release 32 bit: 2652 ms

Aucun commentaire:

Enregistrer un commentaire