[Closed] Proper way of getting first bit in bitarray
What is the fastest way of getting the first bit that is set in a bitarray?
To put this in context:
I’m currently working on a script where I need to get the first edge of every element in an Editable Poly. Because I have to do this on each element the time adds up quite a lot.
Let’s say I have the following bittarray:
theBitArray = #{56..10000}
I have tested the following methods, are there perhaps other methods?
(Faster to slower, in that order)
theBit = (theBitArray as array)[1]
3.879 seconds
theString = theBitArray as string
theBit = (substring theString 3 ((findString theString "..") - 3)) as integer
4.151 seconds
(oh god, I know… ugly! )
theBit = for n in theBitArray do exit with n
4.845 seconds
I think this one takes the most time because of “exit”. I also get a “garbage collection” error. :argh:
I’ll be posting the whole script later on so I can hopefully get some additional feedback from you guys
aha! Thanks! This is checking every index to see if it is 1 and then returns the bit, correct?
I have tried your function and it seems to be slower than converting it to an array and getting the first integer. I have however noticed different times depending if it’s a cold 3dsMax start or not. Strange.
I think I’ll better open a new thread so we can have a look at the whole script. But I’ll do that tomorrow, first some sleep!
theBitArray = #{56..1002}
theBit = theBitArray.count
Like this? too easy I think…
fn lastBit arr =
(
local b
for n in arr while (b = n; on) do ()
b
)
Like so?
try to check the performance…
by the way… we need new test array…
like this:
arr = #{}
seed 0
count = 10000
for k=1 to count do arr[random 1 (count*100)] = on
arr
this array will kill any method that uses to string casting
as you’ll wotk with edges, i not tesed the speed, but worth to try –
$foo.selectedEdges[1].index