For years I have emptied arrays by just setting each element to 0 with For loops. That's time consuming for big arrays and today when I was looking at some old code I wrote a couple of years ago I ran into those lines, and I thought that there must be a much better way to do this, so I started thinking… And yes, of course there is!
How many times didn't I scratch my head when my array is suddenly empty when I forgot ”Preserve” in ReDim? So why not just forget it by purpose…? Here's my example code: REM ***** BASIC ***** Option Explicit Sub Main Const MaxElements=1000000 ' Create an array. Dim A(1 To MaxElements) As Integer Dim i As Long ' Fill the array with some data. Randomize For i=1 To MaxElements A(i)=Rnd*1000 Next i ' Empty the array. Dim Time As Long Time=GetSystemTicks() For i=1 To MaxElements A(i)=0 Next i Print GetSystemTicks()-Time ' Fill it again. For i=1 To MaxElements A(i)=Rnd*1000 Next i ' Empty it again. Time=GetSystemTicks() ReDim A(1 To MaxElements) As Integer Print GetSystemTicks()-Time End Sub Running this, two message boxes (from the Print command) pops upp. Typically the first one displays around 14000 (14213 when I tested a few seconds ago) and the second one around 600 – 800 (797 in this case, 638 is the lowest value so far). After trying a couple of times it seems like the ReDim statement is about 20 times faster than the For loop. It doesn't really matter for small arrays, but the ReDim is a one-liner, For requires three lines. So now I feel really stupid that I didn't think of this before… By the way, the getSystemTicks() function seems to return the time in ms, at least on my system (did some experiments with it a while ago). Try this one, for example: REM ***** BASIC ***** Sub Main Dim A As Long, B As Long, C As Long A=getSystemTicks() Wait 100 B=getSystemTicks() Wait 100 C=getSystemTicks() Print A, B, C End Sub Kind regards Johnny Rosenberg ジョニー・ローゼンバーグ -- ----------------------------------------------------------------- To unsubscribe send email to users-unsubscr...@openoffice.org For additional commands send email to sy...@openoffice.org with Subject: help