1. poke - peek Speed?
- Posted by rforno at tutopia.com
Nov 13, 2002
Rob:
Please see the following snippet:
include machine.e
atom Addr1
Addr1 = allocate(4)
if Addr1 = 0 then
puts(2, "Error - cannot allocate memory!\n")
abort(1)
end if
function to2bytes(integer u)
return and_bits(u, #FF) & floor(u / 256)
end function
function to2bytesa(integer u)
poke4(Addr1, u)
return peek({Addr1, 2})
end function
constant MAX = power(2, 16) - 1
integer r
sequence s
atom t0
constant ITER = 10000000
t0 = time()
for i = 1 to ITER do
r = rand(MAX)
s = to2bytes(r)
end for
? time() - t0
t0 = time()
for i = 1 to ITER do
r = rand(MAX)
s = to2bytesa(r)
end for
? time() - t0
I will use it to write to a file values less than 32768 in byte format. That
is why I use integer and not atom arguments.
Why does the poke - peek version take more time than the other, that uses
division? Using allocate_low instead of allocate, it even takes more time!
TIA.
2. Re: poke - peek Speed?
- Posted by Robert Craig <rds at RapidEuphoria.com>
Nov 13, 2002
-
Last edited Nov 14, 2002
Ricardo Forno writes:
> function to2bytes(integer u)
> return and_bits(u, #FF) & floor(u / 256)
> end function
>
> function to2bytesa(integer u)
> poke4(Addr1, u)
> return peek({Addr1, 2})
> end function
> Why does the poke - peek version take more time
> than the other, that uses division?
I got 4.12 seconds for the first (10 million iterations, including
calling rand() each time), and 5.71 seconds for the second.
floor() and divide are combined into a single integer divide.
and_bits() is very fast. {Addr1, 2} looks like it's free,
but it isn't - a sequence is created each time - you should
store {Addr1, 2} in a variable before looping.
> Using allocate_low instead of allocate,
> it even takes more time!
allocate_low() involves a DOS interrupt which
might slow things down a bit compared to allocate().
Regards,
Rob Craig
Rapid Deployment Software
http://www.RapidEuphoria.com