Arrays in Rev (long)
Richard Gaskin
ambassador at fourthworld.com
Mon Jul 12 23:41:16 EDT 2004
Troy Rollins wrote:
>
> On Jul 12, 2004, at 10:10 PM, J. Landman Gay wrote:
>
>> If speed doesn't matter, then base your storage methods on
>> convenience. If speed does matter, use variables.
>
> Alright. Noted. (Though Richard's article on stack properties suggested
> otherwise.)
It depends on what you mean by "slow".
Yes, it takes almost six times longer to access a property than it does
to access a local var. Run the benchmarking script below; on my modest
single-processor G4 it gets:
Props: 62 ms
Vars: 12 ms
But step back from relative comparisons and look at absolutes: that
script runs 10,000 iterations, which means a single property access
takes only 0.0062 milliseconds. In most real-world operations that's
plenty fast enough. ;)
But then there's the question of what you might do with the values in a
var and how you might access them.
The example in my script below uses only a single variable containing a
single value. This is by far the fastest use of vars, but not
necessarily reflective of the types of things people commonly store in
them, which may also include delimited data that must be parsed.
While using delimiters to parse data is fast, in my earlier benchmarks
it's only faster than using array elements in one specific case: where
you need to walk through the entire list sequentially.
In most real-world usages you're likely interested in accessing specific
items; if you benchmark random-numbered array element access vs. getting
a random-numbered line you'll find array accesses are about two or three
times faster. This is along the lines of what we might expect, as
arrays are indexed for quick lookups while "get line 100 of tMyChunk"
requires that the engine count 100 line delimiters to find your data.
Also, keep in mind that you can convert arrays into chunks and back
again with the split and combine commands. So if most of your code
accesses single elements you can work with the data in an array, but if
you have an operation in which you need to access all elements
sequentially you can trim about 15-20% of execution time by converting
it to a block of text for that routine.
Moral: with Rev, "slow" is very relative. Perhaps the only truly slow
storage is field access, and even those benchmark faster in Rev that
most other xTalks (though much slower than vars or props).
In brief, use whichever you're comfortable with and chances are your
users will never know the difference. :)
--
Richard Gaskin
Fourth World Media Corporation
___________________________________________________
Rev tools and more: http://www.fourthworld.com/rev
-------------------
on mouseUp
put 10000 into N
--
put the millisecs into s
repeat n
set the uTest of this stack to "hello world"
get the uTest of this stack
end repeat
put the millisecs - s into s1
--
put the millisecs into s
repeat n
put "Hello World" into tMyVar
get tMyVar
end repeat
put the millisecs - s into s2
--
put "Props: "&s1 &cr& "Vars: "& s2
end mouseUp
More information about the use-livecode
mailing list