Greetings,
just for fun I have coded this nsieve-prog. Initially I used GlobalAlloc/GlobalFree to allocate memory for the byte-array.
Our friends at MSDN say:
QuoteGlobalAlloc Function
Allocates the specified number of bytes from the heap. .....
New applications should use the heap functions unless documentation states that a global function should be used. .....
Therefore, GlobalAlloc and LocalAlloc have greater overhead than HeapAlloc.
colbold's nsieve.asm with GlobalAlloc:
C:\Asm>nsieve 10
Primes up to 10240000 679461
Primes up to 5120000 356244
Primes up to 2560000 187134
3094 ms
colbold's nsieve.asm with HeapAlloc:
Primes up to 10240000 679461
Primes up to 5120000 356244
Primes up to 2560000 187134
3124 ms
So, using GlobalAlloc is 30 ms faster than HeapAlloc (10 ms per Alloc/Free pair), whereas MSDN states otherwise?
What do you experts out there advise as memory allocation strategy?
P.S.:
; The Computer Language Shootout
; http://shootout.alioth.debian.org/
;Contributed by Klaus Kolba
;Nsieve
include \masm32\include\masm32rt.inc
.DATA
cmd db 128 dup(?)
fmtStr db "Primes up to %8u %8u",13,10,0
align 4
count dd ?
hHeap dd ?
.CODE
nsieve PROC
push ebx
push ecx
invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,edi
;invoke GetProcessHeap
;mov hHeap,eax
;invoke HeapAlloc,eax,HEAP_ZERO_MEMORY,edi
mov esi,eax
xor edx,edx;mov count,0
mov ebx,2
ALIGN 4
.WHILE ebx < edi
.IF BYTE PTR [esi+ebx] == 0
inc edx;count
mov eax,ebx
shl eax,1
mov ecx,eax
.WHILE ecx < edi
mov BYTE PTR [esi+ecx],1
add ecx,ebx
.ENDW
.ENDIF
inc ebx
.ENDW
invoke crt_printf,ADDR fmtStr,edi,edx ;edx=count
invoke GlobalFree,esi
;invoke HeapFree,hHeap,HEAP_NO_SERIALIZE,esi
pop ecx
pop ebx
ret
nsieve ENDP
start:
;invoke GetCurrentProcess
;invoke SetPriorityClass,eax,HIGH_PRIORITY_CLASS
invoke GetTickCount
push eax
invoke GetCL,1,ADDR cmd
invoke atodw,ADDR cmd
mov ecx,eax
xor ebx,ebx
ALIGN 4
.WHILE ebx <= 2
mov eax,10000
sub ecx,ebx
shl eax,cl
add ecx,ebx
mov edi,eax
invoke nsieve
inc ebx
.ENDW
invoke GetTickCount
pop ebx
sub eax,ebx
print str$(eax)," ms"
invoke ExitProcess,0
end start