[REBOL] Who is that grouch? -or- Fun with functions! Re:(3)
From: joel:neely:fedex at: 5-Oct-2000 12:00
Thanks to Allen (again!) for making me think...
-jn-
[joel--neely--fedex--com] wrote:
> [allen--rebolforces--com] wrote:
> >
> > Using the 'repeat native is quicker. (by about 1 sec per 1000
> > iterations on my machine).
> >
>
> Motivated by your comment, I ran a quick benchmark test:
>
> >> bloop/run 50000
> repeat: 8 1
> loop: 25 3.125
> while: 26 3.25
> for: 30 3.75
>
> where the second column is raw times (on a slow box) and the
> third column is times relative to the repeat version...
>
I've rerun the benchmark a few times (on a faster box) with the
-- somewhat interesting -- results aggregated into a table...
loops --> 50000 100000 200000
repeat: 3 1.00 13 1.00 51 1.00
loop: 12 4.00 50 3.85 200 3.92
while: 14 4.67 53 4.08 203 3.98
for: 14 4.67 53 4.08 207 4.06
...from which two observations:
1) The relative times appear to stabilize as the elapsed times
get long enough to overcome round-off error.
2) This is clearly a non-linear process! Hmmm... Could the fact
that we're doing append on a non-pre-allocated block have
anything to do with it? Let's see!
Changing the little benchmark to preallocate the (known in advance,
due to the nature of the result!) necessary number of slots in the
result block (code given at the end of this note), gives us the
following performance on the same box as above:
loops --> 200000 500000 1000000 2000000
repeat: 4 1.00 7 1.00 14 1.00 35 1.00
loop: 6 1.50 15 2.14 32 2.29 66 1.89
while: 7 1.75 18 2.57 35 2.50 74 2.11
for: 12 3.00 29 4.14 60 4.29 135 3.86
Notice that the loop count in this table STARTS where the other
one ended! Now I have to figure out how to stand in a circle
and kick myself. I should have known better! Duuuhhh...
-jn-
REBOL []
bloop: make object! [
t0: t1: t2: t3: t4: now/time
dsec: func [b [time!] e [time!]] [
e: e - b
e/hour * 60 + e/minute * 60 + e/second
]
run: func [n [integer!] /local b i] [
t0: now/time
b: make block! n
repeat i n [append b i: i + 1]
t1: now/time
b: make block! n
i: 0
loop n [append b i: i + 1]
t2: now/time
b: make block! n
i: 0
while [i < n] [append b i: i + 1]
t3: now/time
b: make block! n
for i 1 n 1 [append b i]
t4: now/time
print [
"repeat: " t0: dsec t0 t1 tab t0 / t0 newline
" loop: " t1: dsec t1 t2 tab t1 / t0 newline
" while: " t2: dsec t2 t3 tab t2 / t0 newline
" for: " t3: dsec t3 t4 tab t3 / t0
]
]
]