Hi guys,
I've often heard that memblocks are super fast but I don't know anyone who's actually gone out of their way to test their speed. So I pulled a
fractal program from the snippets board, and did some experiments and optimization on the code I found. I quickly discovered that moving the sync command out of the loop dramatically sped the program up by as much as 10 times. I then proceeded to test memblocks against the dot command in three different ways.
1. Dot command without unlocking and locking pixels
2. Dot command with unlocking and locking pixels
3. Memblock command
The results are as follows:
1. DOT command without unlocking = 610ms to 630ms
2. DOT command with unlocking = 157ms to 177ms
3. Memblock method = 149ms to 151ms
So it appears that memblocks are indeed a little bit faster.
But the question is, can you make this program run any faster?
Memblock version
Sync On
Sync : Sync
Global width as DWORD
Global height as DWORD
Global bit as DWORD
c as dword
make memblock from bitmap 1, 0
width = memblock dword(1, 0)
height = memblock dword(1, 4)
bit = memblock dword(1, 8)
time = timer()
for y= 0 to 479
for x= 0 to 639
a#=0.0
c#=0.0
s#=x/100.0-3.0
t#=y/80.0-3.0
n=0
repeat
inc n
h#=s#*s#
k#=t#*t#
c#=s#*(h#-3.0*k#)+0.5
a#=t#*(3.0*h#-k#)
t#=a#
s#=c#
until n=16 or fastabs(c#)>9.0 or fastabs(a#)>9.0
if fastabs(c#)<9.0 or fastabs(a#)<9.0
select n
case 1 : c=rgb(255,255,255) : endcase
case 2 : c=rgb(255,255,0): endcase
case 3 : c=rgb(255,0,255): endcase
case 4 : c=rgb(0,255,255): endcase
case 5 : c=rgb(255,0,0): endcase
case 6 : c=rgb(0,255,0): endcase
case 7 : c=rgb(0,0,255): endcase
case 8 : c=rgb(255,200,0): endcase
case 9 : c=rgb(0,255,200): endcase
case default : c=rgb(0,0,0) : endcase
endselect
memloc = XYConvert(x, y)
write memblock dword 1, memloc, c
endif
next x
next y
make bitmap from memblock 0, 1
sync
totaltime = timer() - time
Set Window Title str$(totaltime)
wait key
end
function fastabs(n#)
if n# < 0 then n# = -n#
endfunction n#
Function XYConvert(a, b)
Offset = 12
a = a * 4
b = b * width * 4
output = Offset + a + b
EndFunction output
Dot with Unlocked pixels
sync on : sync rate 0
time = timer()
sync : sync
c as dword
for y= 0 to 479
lock pixels
for x= 0 to 639
a#=0.0
c#=0.0
s#=x/100.0-3.0
t#=y/80.0-3.0
n=0
repeat
inc n
h#=s#*s#
k#=t#*t#
c#=s#*(h#-3.0*k#)+0.5
a#=t#*(3.0*h#-k#)
t#=a#
s#=c#
until n=16 or fastabs(c#)>9.0 or fastabs(a#)>9.0
if fastabs(c#)<9.0 or fastabs(a#)<9.0
select n
case 1 : c=rgb(255,255,255) : endcase
case 2 : c=rgb(255,255,0): endcase
case 3 : c=rgb(255,0,255): endcase
case 4 : c=rgb(0,255,255): endcase
case 5 : c=rgb(255,0,0): endcase
case 6 : c=rgb(0,255,0): endcase
case 7 : c=rgb(0,0,255): endcase
case 8 : c=rgb(255,200,0): endcase
case 9 : c=rgb(0,255,200): endcase
case default : c=rgb(0,0,0) : endcase
endselect
dot x,y,c
endif
next x
unlock pixels
next y
sync
totaltime = timer() - time
Set Window Title str$(totaltime)
wait key
end
function fastabs(n#)
if n# < 0 then n# = -n#
endfunction n#
Dot without unlocked pixels
sync on : sync rate 0
time = timer()
sync : sync
c as dword
for y= 0 to 479
for x= 0 to 639
a#=0.0
c#=0.0
s#=x/100.0-3.0
t#=y/80.0-3.0
n=0
repeat
inc n
h#=s#*s#
k#=t#*t#
c#=s#*(h#-3.0*k#)+0.5
a#=t#*(3.0*h#-k#)
t#=a#
s#=c#
until n=16 or fastabs(c#)>9.0 or fastabs(a#)>9.0
if fastabs(c#)<9.0 or fastabs(a#)<9.0
select n
case 1 : c=rgb(255,255,255) : endcase
case 2 : c=rgb(255,255,0): endcase
case 3 : c=rgb(255,0,255): endcase
case 4 : c=rgb(0,255,255): endcase
case 5 : c=rgb(255,0,0): endcase
case 6 : c=rgb(0,255,0): endcase
case 7 : c=rgb(0,0,255): endcase
case 8 : c=rgb(255,200,0): endcase
case 9 : c=rgb(0,255,200): endcase
case default : c=rgb(0,0,0) : endcase
endselect
dot x,y,c
endif
next x
next y
sync
totaltime = timer() - time
Set Window Title str$(totaltime)
wait key
end
function fastabs(n#)
if n# < 0 then n# = -n#
endfunction n#
My system specs here:
Windows 7 64-bit Premium
Sempron 145 @ 2.8Ghz core unlocked to AMD Athlon II X2 4400e @ 3.06Ghz
nVidia 8800GTS 320Mb