April 25, 2024, 01:55:23 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


ZeroMemory

Started by Brian, November 09, 2017, 06:55:27 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

November 13, 2017, 04:30:41 AM #25 Last Edit: November 13, 2017, 04:44:07 AM by Andy
Brian,

Thanks for your PC spec, much faster than my AMD setup.

Anyway, I was curious and tried it on Anne's laptop (much faster machine), the results were the same - pointers being by far the quickest.

I've amended the program to test the three methods 10 times and display the slowest, fastest and average time for each - you can clearly see rtlzeromemory is the slowest as you have to wait a second or two before it returns it's results.

Every time pointers win outright no matter on the spec on the machine.


$include "windowssdk.inc"
DECLARE CDECL EXTERN _memset(dst AS POINTER,value AS CHAR,count AS UINT)

def Freq,StartCount,StopCount:uint64
def TimingmSec,OverHead,Average,Fastest,Slowest:float
def x1,x2,f:pointer

setprecision(4)

int x
int ArraySize = 100000
string a[1000000]

int loops = 0
int maxloops = 10

int P1size = 1000000
pointer P1
P1 = NEW(string,P1size)
settype P1,STRING

Slowest = 0.00
Fastest = 99999999.99

f = Freq: QueryPerformanceFrequency(f)
x1 = StartCount: QueryPerformanceCounter(x1)
   for x = 1 to 1000
   next x
x2 = StopCount: QueryPerformanceCounter(x2)

OverHead = StopCount - StartCount
         
openconsole
print
print "  Array filled with ",ltrim$(USING("#,###,###",ArraySize))," string entries, testing ",maxloops,"times."
print

do
loops ++
for x = 0 to ArraySize
    #p1[x,0] = str$(x)
next x

x1 = StartCount: QueryPerformanceCounter(x1)
delete p1
P1 = NEW(string,P1size)
settype P1,STRING
x2 = StopCount: QueryPerformanceCounter(x2)

TimingmSec = (StopCount - StartCount - OverHead) / Freq * 1000

if TimingmSec > Slowest
   Slowest = TimingmSec
endif

if TimingmSec < Fastest
   Fastest = TimingmSec
endif

Average = Average + TimingmSec

until loops = maxloops

print
print "  Pointer slowest (",Slowest," ) millisecs"
print "  Pointer fastest (",Fastest," ) millisecs"
print
print "  Pointer took on average (",Average / maxloops," ) millisecs"
print "  ----------------------------------------------------------------------"

'---------------------------------------------

loops = 0
Slowest = 0.00
Fastest = 99999999.99
Average = 0.00

do
loops ++

for x = 0 to ArraySize
    a[x] = str$(x)
next x

if ArraySize > 500000 'If number of array entries is greater than half of the array size.
   x1 = StartCount: QueryPerformanceCounter(x1)
for x = 0 to 1000000
a[x] = ""
next x
else
   x1 = StartCount: QueryPerformanceCounter(x1)
for x = 0 to 1000000
if a[x] = "" then breakfor
a[x] = ""
next x
endif

x2 = StopCount: QueryPerformanceCounter(x2)
TimingmSec = (StopCount - StartCount - OverHead) / Freq * 1000

if TimingmSec > Slowest
   Slowest = TimingmSec
endif

if TimingmSec < Fastest
   Fastest = TimingmSec
endif

Average = Average + TimingmSec

until loops = maxloops

print
print "  For/Next slowest (",Slowest," ) millisecs"
print "  For/Next fastest (",Fastest," ) millisecs"
print
print "  For/Next took on average (",Average / maxloops," ) millisecs"
print "  ----------------------------------------------------------------------"

'---------------------------------------------

loops = 0
Slowest = 0.00
Fastest = 99999999.99
Average = 0.00

do
loops ++

for x = 0 to ArraySize
    a[x] = str$(x)
next x

x1 = StartCount: QueryPerformanceCounter(x1)
rtlzeromemory(a,sizeof(a))
x2 = StopCount: QueryPerformanceCounter(x2)
TimingmSec = (StopCount - StartCount - OverHead) / Freq * 1000

if TimingmSec > Slowest
   Slowest = TimingmSec
endif

if TimingmSec < Fastest
   Fastest = TimingmSec
endif

Average = Average + TimingmSec

until loops = maxloops

print
print "  rtlzeromemory slowest (",Slowest," ) millisecs"
print "  rtlzeromemory fastest (",Fastest," ) millisecs"
print
print "  rtlzeromemory took on average (",Average / maxloops," ) millisecs"
print "  ----------------------------------------------------------------------"


print
print
print "  Press any key to exit..."
print

delete p1

do:until inkey$ <> ""
closeconsole
end


BTW: Remember recently we were looking at sorting and Clint came up with his bubblesort4optimised code which returned 0 millisecs to sort? well I've since gone back to it and timed it again using Grahams code - it actually takes around 1.019 millisecs - but still fantastic.

Fasecero's won the race at 0.026!!!

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

jalih

Quote from: Andy
Anyway, I was curious and tried it on Anne's laptop (much faster machine), the results were the same - pointers being by far the quickest.

I've amended the program to test the three methods 10 times and display the slowest, fastest and average time for each - you can clearly see rtlzeromemory is the slowest...

Note, that security wise the three methods are not the same. Releasing memory thru pointer doesn`t zero the memory content. Same is when assigning string as empty inside a loop.

Brian

November 13, 2017, 04:51:11 AM #27 Last Edit: November 13, 2017, 04:52:56 AM by Brian Pugh
Good stuff, Andy,

As before, pointers won out at an average of 2.43, for/next at 9.11, and rtlzero at 15.85. Great timing code, as well, I can see a use for that in testing code

Jalih, as Bill pointed out to me, you would only be bothered about securely erasing all the variable if it contained sensitive code, like passwords, or banking codes, perhaps

Brian


Andy

Thanks Brian, you can call me Sherlock in future if you wish lol!

Jalih - It's a good point you make too, for the most part we are just using arrays for general ever day use, but something to note if you have sensitive data.

Andy (aka Sherlock Holmes).  ;D


Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.