If you want to test the speed of the dll calls, it was better removing the other 2 commands from the inner loop. In my PC it sped up the drawing from 0.73s to 0.58s. Also, the loop itself has an overhead, which we can reduce (for the tests) by partial unrolling:
Code:
Loop 255 {
x++
y = 0
loop 17 {
;y++
;color := (b << 16) | (y << 8) | x
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y,Int,color)
}
}
It reduced the running time slightly (from 0.58s to 0.57s), proving that not the loop, but these dllcall’s are indeed very slow.
One can try making the type qualifiers literal strings:
Code:
DllCall("SetPixel","Int",hdc,"UChar",x,"UChar",y,"Int",color)
This does not bring any significant change in speed.
For keeping the functionality, you can put the assignments inside the dllcall’s, although the order of parameter evaluation is not specified in AHK, therefore it may not work in a future version. Also, a few percent speedup is possible if the dllcall’s are separated by commas, making them parts of a single expression. The inner loop is:
Code:
loop 17 {
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(b<<16)|(y<<8)|x)
}
These bring a speedup of 14% only (0.73/0.64), by removing most overhead outside of the dllcall’s.
In this particular example another couple of percents speedup is possible with moving the unchanging part of the expression outside of the inner loop. All together 20% (0.73s-->0.61s):
Code:
Loop 255 {
x++
z := (b<<16)|x
y = 0
loop 17 {
DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
,DllCall("SetPixel",Int,hdc,UChar,x,UChar,y++,Int,(y<<8)|z)
}
}