Clear/Set Array

BlitzMax Forums/BlitzMax Programming/Clear/Set Array

BLaBZ(Posted 2010) [#1]
How would I go about setting all the values in an array quickly?
Or setting all the values back to 0?


ima747(Posted 2010) [#2]
theArray = new int[Len(theArray)]

Might be a faster way but that should be pretty quick to reset to 0... If you know the length you can obviously replace the Len call with a number for more zip.


Blueapples(Posted 2010) [#3]
Hmm, I don't think triggering malloc() and a garbage collection cycle is the fastest way to clear an int array.

Something with Banks maybe?

The problem with anything *other than* creating a whole new block of memory, and the problem with settings all values in an array quickly, is that it will always be O(n) bound. There's simply no way around that, unless you can use some sort of trickery to make the code behave "as if" the array were blank, or have multiple arrays in memory with the values preset, if appropriate.

It really depends on what you're trying to do, honestly.


Blueapples(Posted 2010) [#4]
Well kiss my grits, banks are faster. (On my MacBook anyway)

Output:
Assign to arr_a (slow due to Rand()): 288
Assign to arr_b: 8
Copy arr_a to arr_b, loop method: 8
Copy arr_a to arr_c, CopyBank method: 3

To be honest, I have a hard time imagining a situation where 5 milliseconds matters over a million integer assignments. This case is so narrow as to be hardly worthy of comment. The results aren't surprising though since it's doing a direct memory copy.


Muttley(Posted 2010) [#5]
I get these results on my machine (Windows 7 64bit, 2.4GHz Quad Core):

Assign to arr_a (slow due to Rand()): 73
Assign to arr_b: 1
Copy arr_a to arr_b, loop method: 3
Copy arr_a to arr_c, CopyBank method: 4




Czar Flavius(Posted 2010) [#6]
For general usage? ima's suggestion is more than sufficient.


Blueapples(Posted 2010) [#7]
Not really since it didn't answer the part about setting items (presumably to a non-zero value since that was asked for separately).

This appears to not be a universal metric however. Copying an array of integers from one array to another using a simple for loop is a perfectly acceptable method.


ima747(Posted 2010) [#8]
Well I focused on the setting it all to zero aspect since that's the easiest, and is (technically) setting all the values... just to 0... :0)

If you want arbitrary values for each element, or something other than 0, I would personally just use a loop. If you can spare the memory and want more fractions of a millisecond then setting the array = to a preset array, or predefined constant might be better... like blue said it's down to millisecs per million, and if that really matters then I'm afraid blitz isn't what you should be using in the first place.

There's better ways to do things but beyond a certain point your better off re-thinking the whole concept. e.g. when 5 millisecs over the course of a million value changes makes a difference atleast look into C... If speed of development is more important than fractional execution speed then find something in blitz that makes you happy and don't sweat the loss, that's what faster processors are for :0)