Fastest method to compare numbers?

Blitz3D Forums/Blitz3D Programming/Fastest method to compare numbers?

CopperCircle(Posted 2007) [#1]
Hi, I need to compare a list of numbers against one number and find the closest match. What would be the quickest method? Thanks.


jfk EO-11110(Posted 2007) [#2]
a binary search,

Or even faster, but maybe not possible if it's a long list of possible numbers: by index.

I assume you want to do this with 24 bit RGB values that should be converted to eg. 16 bit colors?

Well then index is BASICLY not possibe because this would require a 16 Giabyte lookup table.

However, if you would check r,g and b seperately then it's possible noless and it's real fast.


first create an array with 256 fields, then fill them with the corresponding channel value of the wanted color, eg:

for converting 8 bit channels to 3 bit (0-255 to 0-7 and therefor rgb32 to rgb9) you would need to prepare these arrays:
for i=0 to 255
red(i)=i shr 5
green(i)=i shr 5
blue(i)=i shr 5
next

(that said, I've never seen 9bit gfx displays)

then, in the image conversion you simply have to use them by index:

rgb=readpixelfast (x,y) and $ffffff
r=(rgb and $FF0000) shr 16
g=(rgb and $FF00) shr 8
b=(rgb and $FF)
rgb2=(red(r)shl 16) or (green(g)shl 8) or blue(b)

writepixelfast(x,y,rgb2)

So you see, simply access them by their R,G,B values.


BTW since the arrays red(),green() and blue() contain the same data, you would need only one of them. With 3 of them you still may increase the siginficance of one channel, if you think that's neccessary.