VS2010 64-bit dynamic memory allocation using 'new' operator is very slow

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I am noticing that new operator based dynamic memory allocation is very slow in VS2010 as compare to VS2010s malloc if I use /EHsc compiler option. I am building a 64-bit application on Windows 7 using VS2010.
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64.
Microsoft (R) Incremental Linker Version 10.00.30319.01
If I run a test program new_vs_malloc.cxx, then it gives following result:-
1) cl new_vs_malloc.cxx /MD /O2

H:delme>new_vs_malloc.exe
UseMallocArray = 2 S 325 MS
UseNewArray = 2 S 324 MS

2) cl new_vs_malloc.cxx /MD /EHsc /O2
H:delme>new_vs_malloc.exe
UseMallocArray = 2 S 278 MS
UseNewArray = 6 S 521 MS

<div style="color:Black;background-color:White; <pre>

#include <iostream>
#include <string>
#include <windows.h>

<span style="color:Blue; using <span style="color:Blue; namespace std;
<span style="color:Green; /** Get a time in milliseconds for animation */
<span style="color:Blue; long currentTime()
{
<span style="color:Blue; return(GetTickCount());
}

<span style="color:Blue; class TestTimer {
<span style="color:Blue; public:
TestTimer(<span style="color:Blue; const std::string & name) : name(name)
{
start = currentTime();
}
~TestTimer()
{
<span style="color:Blue; long span = currentTime() - start;
cout<<name<<<span style="color:#A31515; " = "<<(span)/1000 <<<span style="color:#A31515; " S "<<(span)%1000<<<span style="color:#A31515; " MS"<<endl;

}
<span style="color:Blue; private:
std::string name;
<span style="color:Blue; long start;
};

<span style="color:Blue; struct Pixel
{
Pixel() { }

Pixel(<span style="color:Blue; unsigned <span style="color:Blue; char r, <span style="color:Blue; unsigned <span style="color:Blue; char g, <span style="color:Blue; unsigned <span style="color:Blue; char b)
: r(r), g(g), b(b) { }
~Pixel() { }
<span style="color:Blue; void print() {
r1 = <span style="color:Blue; this->r;
g1 = <span style="color:Blue; this->g;
b1 = <span style="color:Blue; this->b;
}

<span style="color:Blue; unsigned <span style="color:Blue; char r, g, b;
<span style="color:Blue; unsigned <span style="color:Blue; char r1,g1,b1;
};

<span style="color:Blue; void UseMallocArray()
{
TestTimer t(<span style="color:#A31515; "UseMallocArray");
<span style="color:Blue; for(<span style="color:Blue; int j = 0; j < 1000; ++j)
{
<span style="color:Blue; int dimension = 999;
Pixel * pixels = (Pixel *)malloc(<span style="color:Blue; sizeof(Pixel) * dimension * dimension);
<span style="color:Blue; for(<span style="color:Blue; int i = 0 ; i < dimension * dimension; ++i)
{
pixels.r = 255;
pixels.g = 0;
pixels.b = 0;
}
free(pixels);
}
}
<span style="color:Blue; void UseNewArray()
{
TestTimer t(<span style="color:#A31515; "UseNewArray");
<span style="color:Blue; for(<span style="color:Blue; int j = 0; j < 1000; ++j)
{
<span style="color:Blue; int dimension = 999;
Pixel * pixels = <span style="color:Blue; new Pixel[dimension * dimension];
<span style="color:Blue; for(<span style="color:Blue; int i = 0 ; i < dimension * dimension; ++i)
{
pixels.r = 255;
pixels.g = 0;
pixels.b = 0;
}
<span style="color:Blue; delete [ ]pixels;
}
}

<span style="color:Blue; int main()
{
TestTimer t1(<span style="color:#A31515; "The whole thing");
UseMallocArray();
UseNewArray();
TestTimer t2(<span style="color:#A31515; "Timer call overhead");
<span style="color:Blue; return 0;
}

[/code]

<br/>
Any idea why it is behaving differently. I tried to remove /EHsc compiler option for my applications source files compilation, but it is still slow.
It could be because there are other thirdparty libraries which are built using /EHsc compiler option, since I am linking my application with those libraries, so it might be changing my application behavior even after removing /EHsc compiler option from my
applications object file compilation.
For me, it is important to know that how I can achieve same performance as I get without /EHsc compiler option. My application is memory intensive, so I can not afford lower performance just because of /EHsc compiler option. Is there any solution
for this. <hr class="sig -Vipin

View the full article
 
Back
Top