Author Topic: HeapAlloc vs HeapReAlloc  (Read 1287 times)

jj2007

  • Member
  • *****
  • Posts: 8841
  • Assembler is fun ;-)
    • MasmBasic
HeapAlloc vs HeapReAlloc
« on: April 01, 2018, 05:16:02 PM »
A comparison of the speed of HeapAlloc/HeapFree vs HeapReAlloc (full code attached, plain Masm32).

First, the alloc/free loop:
Code: [Select]
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:
Code: [Select]
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

Code: [Select]
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

  • Member
  • *****
  • Posts: 1684
Re: HeapAlloc vs HeapReAlloc
« Reply #1 on: April 01, 2018, 06:14:09 PM »
Code: [Select]
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

  • Member
  • *****
  • Posts: 8841
  • Assembler is fun ;-)
    • MasmBasic
Re: HeapAlloc vs HeapReAlloc
« Reply #2 on: April 01, 2018, 06:53:04 PM »
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:
Code: [Select]
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

  • Member
  • *****
  • Posts: 1684
Re: HeapAlloc vs HeapReAlloc
« Reply #3 on: April 01, 2018, 07:34:08 PM »
Hi Jochen,

Win8.1 - 64
Creative coders use backward thinking techniques as a strategy.

FORTRANS

  • Member
  • *****
  • Posts: 1034
Re: HeapAlloc vs HeapReAlloc
« Reply #4 on: April 02, 2018, 12:39:13 AM »
Hi,

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

Code: [Select]
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
« Last Edit: April 02, 2018, 04:14:41 AM by FORTRANS »

caballero

  • Member
  • *****
  • Posts: 1099
    • abre ojos ensamblador
Re: HeapAlloc vs HeapReAlloc
« Reply #5 on: April 02, 2018, 12:48:08 AM »
W7-64bits
Code: [Select]
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?
En un lugar de la Mancha de cuyo nombre no quiero acordarme

- There are 10 kind of people: those who know binary numbers and those who don't -

jj2007

  • Member
  • *****
  • Posts: 8841
  • Assembler is fun ;-)
    • MasmBasic
Re: HeapAlloc vs HeapReAlloc
« Reply #6 on: April 02, 2018, 04:00:06 AM »
These results are pretty confusing :(

Thanks, folks :icon14:

LiaoMi

  • Member
  • ***
  • Posts: 325
Re: HeapAlloc vs HeapReAlloc
« Reply #7 on: April 02, 2018, 04:18:31 AM »
Hi,

win10-64

Code: [Select]
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

  • Member
  • ****
  • Posts: 819
Re: HeapAlloc vs HeapReAlloc
« Reply #8 on: April 02, 2018, 07:01:13 AM »
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

  • Member
  • *****
  • Posts: 8841
  • Assembler is fun ;-)
    • MasmBasic
Re: HeapAlloc vs HeapReAlloc
« Reply #9 on: April 02, 2018, 07:25:19 AM »
Thanks, Rui & LiaoMi. I start wondering if something is wrong with my Core i5/Win7 machine ::)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5899
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: HeapAlloc vs HeapReAlloc
« Reply #10 on: April 02, 2018, 07:52:32 AM »
This was a LAB class thread so I moved it here.  :biggrin:
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 8841
  • Assembler is fun ;-)
    • MasmBasic
Re: HeapAlloc vs HeapReAlloc
« Reply #11 on: April 02, 2018, 09:25:42 AM »
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

  • Member
  • ****
  • Posts: 604
Re: HeapAlloc vs HeapReAlloc
« Reply #12 on: April 02, 2018, 09:43:22 AM »
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

  • Member
  • ****
  • Posts: 970
  • Eagles are just great!
Re: HeapAlloc vs HeapReAlloc
« Reply #13 on: April 02, 2018, 12:34:43 PM »
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.
Felipe.

Fabioxds

  • Regular Member
  • *
  • Posts: 12
Re: HeapAlloc vs HeapReAlloc
« Reply #14 on: April 02, 2018, 01:11:55 PM »
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