I run the following MFC code:
我运行以下MFC代码:
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:
如果我构建64位版本,代码运行速度比32位慢大约2倍。我尝试了Debug和Release版本。时间如下:
Debug 64 bit: 15085 ms
Debug 32 bit: 8128 ms
Release 64 bit: 8237 ms
Release 32 bit: 4695 ms调试64位:15085 ms调试32位:8128 ms版本64位:8237 ms版本32位: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微软的Visual Studio 2005版本8.0.50727.4039(QFE.050727-4000)在Windows Server 2008 R2标准版64位处理器:Intel(R)至强(R)E5645 @ 2.40GHz的内存: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:
@BarmakShemirani,我按照你的建议尝试了矢量,你是对的,结果完全不同。看起来64位代码更快。结果如下:
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调试64位:3563 ms调试32位:4562 ms版本64位:1140 ms版本32位:1563 ms
Here is the optimized version with CArray:
以下是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调试64位:2625 ms调试32位:2625 ms版本64位:1015 ms版本32位:1438 ms
Is CArray growing mechanism causes the code to run slower in 64 bit?
CArray增长机制会导致代码在64位运行速度变慢吗?
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:
我查看了CArray增长的代码,基本上它分配了一个新的数组,并且每1024个添加的元素就会复制旧的内容。所以我只是使用以下结果模拟分配代码:
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调试64位:10483 ms调试32位:4696 ms版本64位:5803 ms版本32位:2652 ms
1 个解决方案
#1
In 64-bit mode, pointers are twice as big. So it can happen that you have to allocate up to twice as much memory in the 64-bit version, just to store your pointers. This might be enough in itself to slow the program down.
在64位模式下,指针是两倍大。因此,您可能需要在64位版本中分配最多两倍的内存,只是为了存储指针。这本身就足以减慢程序的速度。
#1
In 64-bit mode, pointers are twice as big. So it can happen that you have to allocate up to twice as much memory in the 64-bit version, just to store your pointers. This might be enough in itself to slow the program down.
在64位模式下,指针是两倍大。因此,您可能需要在64位版本中分配最多两倍的内存,只是为了存储指针。这本身就足以减慢程序的速度。