I thought using bitshift-operations instead of multiplications should run faster, but seemingly there is no significant difference:
set display mode 800, 600, 32
set window on
sync on
sync rate 0
sync
iterations = 5
times = 1000000
r as byte
b as byte
g as byte
set text font "Tahoma"
set text size 16
cls
sum1 = 0
sum2 = 0
for j = 1 to iterations
print
print "Test ", j, "/", iterations
print " Iterations: ", times : sync
t = timer()
for i = 1 to times
r = i : g = 255-r : b = r+128
temp = argb1(r,g,b,255)
next i
t = timer()-t
inc sum1, t
print " argb1: ", t, " ms" : sync
t = timer()
for i = 1 to times
r = i : g = 255-r : b = r+128
temp = argb2(r,g,b,255)
next i
t = timer()-t
inc sum2, t
print " argb2: ", t, " ms" : sync
times = times*2
if escapekey() then end
next j
fac# = sum1/(1.0*sum2)
print "___________________________"
print
print "Result:"
print " argb1: ", sum1, " ms"
print " argb2: ", sum2, " ms"
print
print " Factor: ", str$(fac#,3)
if fac# < 1.0 then print " - argb1 wins!" else print " - argb2 wins!"
repeat
sync
until escapekey()
end
function argb1(r, g, b, a)
col as dword : col = ((a * 256 + r) * 256 + g) * 256 + b
endfunction col
function argb2(r, g, b, a)
col = a<<24 || r<<16 || g<<8 || b
endfunction col
Anyway. Given the fact that the argb-function won't be used too often probably and it should already be fast enough, I highly doubt that optimizations on this level have any noticable effect on a game or application at all. I did this test just out of curiousity, you know.
Edit:
Actually there is quite a difference. When reducing the overhead generated by the for-loop and variable-assignments, the difference gets more obvious and the function using bitwise-operators gets around ~30% faster (on my computer at least).
set display mode 800, 600, 32
set window on
sync on
sync rate 0
sync
iterations = 5
times = 100000
r as byte
b as byte
g as byte
set text font "Tahoma"
set text size 16
cls
sum1 = 0
sum2 = 0
for j = 1 to iterations
print
print "Test ", j, "/", iterations
print " Iterations: ", times : sync
t = timer()
for i = 1 to times
r = i : g = 255-r : b = r+128
temp = argb1(r,g,b,255) : temp = argb1(r,g,b,255) : temp = argb1(r,g,b,255) : temp = argb1(r,g,b,255)
temp = argb1(r,g,b,255) : temp = argb1(r,g,b,255) : temp = argb1(r,g,b,255) : temp = argb1(r,g,b,255)
temp = argb1(r,g,b,255) : temp = argb1(r,g,b,255) : temp = argb1(r,g,b,255) : temp = argb1(r,g,b,255)
next i
t = timer()-t
inc sum1, t
print " argb1: ", t, " ms" : sync
if escapekey() then end
t = timer()
for i = 1 to times
r = i : g = 255-r : b = r+128
temp = argb2(r,g,b,255) : temp = argb2(r,g,b,255) : temp = argb2(r,g,b,255) : temp = argb2(r,g,b,255)
temp = argb2(r,g,b,255) : temp = argb2(r,g,b,255) : temp = argb2(r,g,b,255) : temp = argb2(r,g,b,255)
temp = argb2(r,g,b,255) : temp = argb2(r,g,b,255) : temp = argb2(r,g,b,255) : temp = argb2(r,g,b,255)
next i
t = timer()-t
inc sum2, t
print " argb2: ", t, " ms" : sync
times = times*2
if escapekey() then end
next j
fac# = sum1/(1.0*sum2)
print "___________________________"
print
print "Result:"
print " argb1: ", sum1, " ms"
print " argb2: ", sum2, " ms"
print
print " Factor: ", str$(fac#,3)
if fac# < 1.0 then print " - argb1 wins!" else print " - argb2 wins!"
repeat
sync
until escapekey()
end
function argb1(r, g, b, a)
col as dword : col = ((a * 256 + r) * 256 + g) * 256 + b
endfunction col
function argb2(r, g, b, a)
col = a<<24 || r<<16 || g<<8 || b
endfunction col
