News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

HeapAlloc vs HeapReAlloc

Started by jj2007, April 01, 2018, 05:16:02 PM

Previous topic - Next topic

jj2007

A comparison of the speed of HeapAlloc/HeapFree vs HeapReAlloc (full code attached, plain Masm32).

First, the alloc/free loop:
push [esi+4*ebx] ; old pointer
push rv(nrandom, 1000)    ; get a random number
mov [esi+4*ebx], rv(HeapAlloc, procHeap, HEAP_GENERATE_EXCEPTIONS, eax)
pop edx
invoke lstrcpyn, [esi+4*ebx], offset somestring, edx ; fill the string
pop ecx
jecxz @F
invoke HeapFree, procHeap, 0, ecx
       @@: inc ebx


Second, the realloc loop: push rv(nrandom, 1000)    ; get a random number
mov [esi+4*ebx], rv(HeapReAlloc, procHeap, HEAP_GENERATE_EXCEPTIONS, [esi+4*ebx], eax)
pop edx
invoke lstrcpyn, [esi+4*ebx], offset somestring, edx ; fill the string
inc ebx


alloc#
+94 ms
+468 ms
+826 ms
+390 ms
+624 ms
+297 ms
+468 ms
+437 ms
+406 ms
+265 ms

realloc
+94 ms
+78 ms
+78 ms
+78 ms
+78 ms
+78 ms
+78 ms
+78 ms
+78 ms
+78 ms
ok?

Siekmanski

alloc
+47 ms
+47 ms
+47 ms
+47 ms
+31 ms
+31 ms
+47 ms
+31 ms
+47 ms
+32 ms

realloc
+46 ms
+32 ms
+47 ms
+31 ms
+47 ms
+47 ms
+31 ms
+47 ms
+31 ms
+47 ms
ok?
Creative coders use backward thinking techniques as a strategy.

jj2007

Interesting, Marinus. Which Windows version? My results above are Win7-64, but testing it now on Win10 (and a much slower machine) delivers something different; so it seems M$ has improved HeapAlloc:alloc
+125 ms
+109 ms
+109 ms
+94 ms
+94 ms
+94 ms
+93 ms
+110 ms
+94 ms
+93 ms

realloc
+110 ms
+109 ms
+156 ms
+141 ms
+125 ms
+125 ms
+109 ms
+110 ms
+125 ms
+109 ms

Siekmanski

Creative coders use backward thinking techniques as a strategy.

FORTRANS

#4
Hi,

   In honor of April 1st, here are some results.

Redirect to file, P-III, W2k
alloc
+311 ms
+751 ms
+300 ms
+300 ms
+301 ms
+561 ms
+300 ms
+301 ms
+600 ms
+301 ms

realloc
+691 ms
+360 ms
+641 ms
+341 ms
+651 ms
+340 ms
+661 ms
+341 ms
+651 ms
+340 ms
ok?

Screen capture
F:\TEMP\TEST>heaptest
alloc
+321 ms
+310 ms
+301 ms
+300 ms
+301 ms
+300 ms
+300 ms
+301 ms
+310 ms
+301 ms

realloc
+380 ms
+361 ms
+350 ms
+341 ms
+340 ms
+341 ms
+340 ms
+341 ms
+340 ms
+341 ms
ok?

Screen capture P-II MMX, W98
alloc
+731 ms
+1015 ms
+901 ms
+959 ms
+937 ms
+842 ms
+880 ms
+880 ms
+875 ms
+900 ms

realloc
+5747 ms
+16980 ms
+22631 ms
+24967 ms
+26686 ms
+27869 ms
+28271 ms
+28124 ms
+28596 ms
+28670 ms
ok?

Screen capture, Pentium M, WinXP
A:\>heaptest
alloc
+200 ms
+140 ms
+190 ms
+181 ms
+190 ms
+190 ms
+180 ms
+191 ms
+190 ms
+190 ms

realloc
+211 ms
+250 ms
+210 ms
+211 ms
+200 ms
+200 ms
+211 ms
+210 ms
+210 ms
+210 ms
ok?


Cheers,

Steve

avcaballero

W7-64bits

alloc
+63 ms
+62 ms
+63 ms
+62 ms
+62 ms
+63 ms
+62 ms
+47 ms
+78 ms
+62 ms

realloc
+63 ms
+62 ms
+78 ms
+63 ms
+62 ms
+62 ms
+78 ms
+63 ms
+62 ms
+63 ms
ok?

jj2007

These results are pretty confusing :(

Thanks, folks :icon14:

LiaoMi

Hi,

win10-64

alloc
+31 ms
+47 ms
+31 ms
+31 ms
+31 ms
+32 ms
+31 ms
+31 ms
+31 ms
+47 ms

realloc
+32 ms
+31 ms
+47 ms
+31 ms
+31 ms
+47 ms
+31 ms
+32 ms
+31 ms
+31 ms
ok?

RuiLoureiro

Here it is Jochen
alloc
+438 ms
+219 ms
+187 ms
+203 ms
+157 ms
+171 ms
+141 ms
+203 ms
+187 ms
+157 ms

realloc
+156 ms
+187 ms
+172 ms
+172 ms
+188 ms
+265 ms
+266 ms
+187 ms
+250 ms
+203 ms
ok?

jj2007

Thanks, Rui & LiaoMi. I start wondering if something is wrong with my Core i5/Win7 machine ::)

hutch--

This was a LAB class thread so I moved it here.  :biggrin:

jj2007

Well done, Hutch. I thought it would be a Campus demo showing that reallocating is more efficient, but it turns out that it is pretty unclear what's happening there.

Theory would suggest that allocating a new buffer, then deleting the old one would be much slower; simply because with HeapReAlloc, the OS can often reuse the existing buffer, either because the new string is shorter, or because there were some unused bytes at the tail.

But most posts here show that actually there is no speed difference. Except on my machine :(

LordAdef

win 10 64 home:

alloc
+47 ms
+31 ms
+31 ms
+31 ms
+32 ms
+47 ms
+31 ms
+31 ms
+31 ms
+32 ms

realloc
+31 ms
+31 ms
+31 ms
+32 ms
+46 ms
+32 ms
+31 ms
+31 ms
+31 ms
+32 ms
ok?

felipe

alloc
+47 ms
+31 ms
+47 ms
+31 ms
+32 ms
+47 ms
+31 ms
+31 ms
+47 ms
+31 ms

realloc
+47 ms
+31 ms
+47 ms
+47 ms
+31 ms
+47 ms
+31 ms
+47 ms
+32 ms
+46 ms
ok?

Win 8.1 64 bits. I5. 8gb of ram.

Fabioxds

alloc
+62 ms
+47 ms
+47 ms
+46 ms
+47 ms
+47 ms
+62 ms
+47 ms
+47 ms
+47 ms

realloc
+62 ms
+63 ms
+46 ms
+63 ms
+62 ms
+47 ms
+62 ms
+63 ms
+47 ms
+93 ms
ok?

Windows 7 x64, Pentium Dual Core E5700 3.0GHz, 4gb of RAM