After analyzing the assembly generated by VC ++ for these two cases, here is what I found. The compiler built in almost everything and generated very similar loops for initialization after memory allocation. In case the vector inner loop looks like this:
013E3FC0 test eax,eax 013E3FC2 je std::_Uninit_def_fill_n<vec2 *,unsigned int,vec2,std::allocator<vec2>,vec2>+19h (13E3FC9h) 013E3FC4 mov dword ptr [eax],edx 013E3FC6 mov dword ptr [eax+4],esi 013E3FC9 add eax,8 013E3FCC dec ecx 013E3FCD jne std::_Uninit_def_fill_n<vec2 *,unsigned int,vec2,std::allocator<vec2>,vec2>+10h (13E3FC0h)
where edx and esi registers were looped out of loop:
00013FB5 xor edx,edx 00013FB7 xor esi,esi 00013FB9 lea esp,[esp]
In the case of new[] inner loop looks like this:
009F1800 mov dword ptr [ecx],0 009F1806 mov dword ptr [ecx+4],0 009F180D add ecx,8 009F1810 dec edx 009F1811 jns main+30h (9F1800h)
The differences are very slight, a few more instructions in the case of vector , but probably also faster than mov from registers. Since in most real cases, constructors do much more than assigning zeros, this difference can hardly be noticeable at all. Thus, the value of this testing is doubtful.
Gene bushuyev
source share