Set all contents of an array to 0
BlitzMax Forums/BlitzMax Programming/Set all contents of an array to 0
| ||
is there a fast way to do this that doesn't include having to set each index individually using a for..next loop? |
| ||
dont think so, iterating through arrays is one of the fastest things that you could possibly do with blitzmax, so it shouldnt be a problem. maybe you could try letting the array be GC'd and create a new one, but that might be slower. |
| ||
It actually is a problem; the array is very large and would need to be reset many times each loop. |
| ||
could you give some more details? maybe the array doesnt have to be set to zero, for example if you have to fill an array with x-coords you just write over the old values and THEN set the rest of the array elements to zero Also make sure the array is local and not global. If it is global turn it into a local temporarily. |
| ||
I haven't tested its speed, but a=a[..0,..0] a=a[..fw+1,..fh+1] works. I think the problem is solved. EDIT: Also make sure the array is local and not global. If it is global turn it into a local temporarily. Yea, that's a better idea than mine. |
| ||
You can do it if it is a possibility to keep one empty Array of the size you need in RAM.SuperStrict Local source:Int[3] Local dest:Int[3] Local psource:Int Ptr Local pdest: Int Ptr psource = source pdest = dest dest[2] = 123 MemCopy(pdest , psource , SizeOf(source) ) Print dest[2] The source array is empty, and gets copied over dest with memcopy. If you can't afford to spend this much memory you can empty the array with multiple memcopies using a smaller 'zero-Array': SuperStrict Local source:Int[3] Local dest:Int[6] Local psource:Int Ptr Local pdest: Int Ptr psource = source pdest = dest dest[5] = 123 MemCopy(pdest+3 , psource , SizeOf(source) ) Print dest[5] |
| ||
You could pair it with a caretaker array of the same size that records the number of times the array has been cleared. Then when you access a particular array position, check the caretaker array. If its value is the same as the number of times it has been cleared, then take that value. If it isn't, set the value to zero first and set the caretaker value to the current reset count. Then to reset the array to zero, simply increase the reset count. In most cases this is horribly inefficient but if you reset the array extremely often (many times a second) and use random-access on its elements infrequently (in relation to its size) it might be faster. You essentially sacrifice element access speed for resetting speed. If you were to implement this, it would be better to encapsulate both arrays and the count in an object and use methods to access it or it will get messy fast. Most likely though, you (or I!) are over-thinking the problem and even the slow method of simply recreating the array would be more than fast enough :) |