[Implemented] AllocateMemory without clearing

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

Trond wrote:ReAllocateMemory() also zeros the memory that is newly allocated.
Oops! Then I misunderstood the docs. Thanks!

Regards, Little John
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

Post by HotteHC »

@ts-soft

I repeated the allocating 500 times.

Code: Select all

#HEAP_CREATE_ALIGN_16 = $00010000
#HEAP_ZERO_MEMORY     = $00000008
Procedure.l MemAlloc(size.l,bZeroMem.l = #False)
	If bZeroMem = #False
	 ProcedureReturn HeapAlloc_( GetProcessHeap_(), #HEAP_CREATE_ALIGN_16, Size );
	EndIf
	ProcedureReturn HeapAlloc_( GetProcessHeap_(), #HEAP_CREATE_ALIGN_16 | #HEAP_ZERO_MEMORY, Size );
EndProcedure

Procedure MemFree(*p)
	If *p <> #Null 
	 HeapFree_( GetProcessHeap_(), 0, *p);
	EndIf
EndProcedure



Global *pixels = #Null
Global count.l,st.l,et.l
#COUNT_LOOP = 500


OpenConsole()

st = ElapsedMilliseconds()

for count = 0 to #COUNT_LOOP
 *pixels = AllocateMemory(256000) ;128*128*24 bit Image
   ;do something
  FreeMEmory(*pixels)
next

et = ElapsedMilliseconds()

PrintN("AllocateMemory : "+Str(et-st))

st = ElapsedMilliseconds()

for count = 0 to #COUNT_LOOP
 *pixels = MemAlloc(256000) ;128*128*24 bit Image
   ;do something
  MemFree(*pixels)
next

et = ElapsedMilliseconds()

PrintN("MemAlloc : "+Str(et-st))

Repeat : until InKey()

CloseConsole()
end
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

+1


If this really increases speed, I would also agree to this idea!

If you poke strings with PokeS(), there will be appended a null automatically; so in most cases there are no safety problems. But even if so - the programer should do what he wants to do.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

HotteHC wrote:@ts-soft

I repeated the allocating 500 times.
31 milliseconds :mrgreen: , and i never allocate memory 500x in one program.
Better optimize your program and don't allocate 500x :wink:
31 milliseconds is not the time to smoke a cigarete :)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

AND51 wrote: If you poke strings with PokeS()
poke and peek a slow, better use pointer, copymemory or others :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Deeem2031
Enthusiast
Enthusiast
Posts: 216
Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:

Post by Deeem2031 »

There is an advantage at no costs so there's no reason to refuse to implement this. And btw. normaly I never need the memory to be zeroed - there are only very rare cases were this is usefull and I guess thats true for most projects.
irc://irc.freenode.org/#purebasic
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

Post by HotteHC »

@TS-Soft

On my Laptop the Results are

-AllocateMemory : 141 ms
-MemAlloc : 0 ms !!!

And to loop this 500 times was only for Testing but I m working on
a Bitmap-Lib/2D-Engine and there I have to use many Memory allocations.

For example.
For now I'm able to load an BMP-Image with my Method realy extrem faster then with the PB-Intenals ImageLoaders.
With AllocateMemory it's equal in speed.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

With allocation of 25600000 bytes in your code, AllocateMemory is faster :mrgreen:
Okay, i think is a feature for slow machines, in most programs make this
no difference.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

Post by HotteHC »

Youre right.

It's strange but the big Speed-Difference is in most Times when i Allocate
an Image of 128*128*24 bit which needs 256000 bytes in Memory.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

ts-soft wrote:
AND51 wrote: If you poke strings with PokeS()
poke and peek a slow, better use pointer, copymemory or others :wink:
Hi ts-soft, i have read that from you several times, and from Trond too, but have you tried if there is a speed difference with the use of Peek-Poke and the use of direct pointer assignations? Just disassemble it and compare :)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Psychophanta wrote:Hi ts-soft, i have read that from you several times, and from Trond too, but have you tried if there is a speed difference with the use of Peek-Poke and the use of direct pointer assignations? Just disassemble it and compare :)

Code: Select all

*Mem.byte = AllocateMemory(200000000)

t1 = ElapsedMilliseconds()
If *Mem
  For I = 0 To 199999999
    PokeB(*Mem + I, I)
  Next
EndIf
t2 = ElapsedMilliseconds() - t1

t3 = ElapsedMilliseconds()
If *Mem
  For I = 0 To 199999999
    *Mem\b = I
    *Mem + SizeOf(byte)
  Next
EndIf
t4 = ElapsedMilliseconds() - t3

MessageRequester("Time:", "Poke: " + Str(t2) + #LF$ + "Pointer: " + Str(t4))
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

ts-soft wrote:
Psychophanta wrote:Hi ts-soft, i have read that from you several times, and from Trond too, but have you tried if there is a speed difference with the use of Peek-Poke and the use of direct pointer assignations? Just disassemble it and compare :)

Code: Select all

*Mem.byte = AllocateMemory(200000000)

t1 = ElapsedMilliseconds()
If *Mem
  For I = 0 To 199999999
    PokeB(*Mem + I, I)
  Next
EndIf
t2 = ElapsedMilliseconds() - t1

t3 = ElapsedMilliseconds()
If *Mem
  For I = 0 To 199999999
    *Mem\b = I
    *Mem + SizeOf(byte)
  Next
EndIf
t4 = ElapsedMilliseconds() - t3

MessageRequester("Time:", "Poke: " + Str(t2) + #LF$ + "Pointer: " + Str(t4))
I got:
Poke: 9578
Pointer: 14531
:wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Psychophanta wrote: I got:
Poke: 9578
Pointer: 14531
:wink:
Please test it without Debugger :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

ts-soft wrote:Please test it without Debugger :wink:
I did it without debugger!
What are your results?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

MessageRequester wrote:---------------------------
Time:
---------------------------
Poke: 1141
Pointer: 812
---------------------------
OK
---------------------------
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Post Reply