Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
sjb64
Roku Guru

Just some experimental variable timings I found interesting...

Tested on a Roku 3

Count=10000000 ' 10 million
X = 1000: SW = Epoch(): for Y = 1 to Count: Z = X: end for: ? Epoch()-SW
X = { X:1000 }: SW = Epoch(): for Y = 1 to Count: Z = X.X: end for: ? Epoch()-SW
m.X = 1000: SW = Epoch(): for Y = 1 to Count: Z = m.X: end for: ? Epoch()-SW
m.Global.AddFields({X: 1000}): SW = Epoch(): for Y = 1 to Count: Z = m.Global.X: end for: ? Epoch()-SW
... code to fill reg variable (as a string)... SW = Epoch(): for Y = 1 to 10,000,000: Z = RegistrySection.Read("X"): end for: ? Epoch()-SW

2.187 seconds, 5.562 seconds, 5.548 seconds, 40.905 seconds, and 45.485 seconds respectively

An AA dereference is less than half the performance of a direct variable, that's not a surprise as it is basically 2 variable references, but global access is almost as the same as registry read performance, 7 times slower than X.X, and 18 times slower than a direct variable, so global variables are rather expensive compared to other AA variables, I am assuming there is serialization/deserialization involved.

None of this matters except in high count tight loops.  Out of habit I hoist global variables (and obviously registry variables) that are accessed more than once in a code block, so will definitely continue doing that in loops.

Epoch code is 

function Epoch() as longinteger
    TS = CreateObject("roDateTime")
    return 1000& * TS.AsSeconds() + TS.GetMilliseconds()
end function
0 Kudos
4 REPLIES 4
Komag
Roku Guru

Re: Just some experimental variable timings I found interesting...

Interesting comparison! Yes, I also hoist global variables for fast use in functions.

0 Kudos
sanity-check
Roku Guru

Re: Just some experimental variable timings I found interesting...

This is interesting, thanks. Also, if anyone ever needs to force a Roku reboot just run this in the render thread. 🙂

0 Kudos
sjb64
Roku Guru

Re: Just some experimental variable timings I found interesting...

Yes I run these on the render thread on a Roku 3, but mega loops like this can absolutely reboot the device..

A few more...

Stamp = CreateObject("roTimeSpan")

Stamp.Mark()
for i=1 to 1000000 ' 1 million
   X={"aaa":1"bbb":2"ccc":3}
end for
Stamp.TotalMilliseconds()

Stamp.Mark()
for i=1 to 1000000
   X={}
   X.Append({"aaa":1"bbb":2"ccc":3})
end for
Stamp.TotalMilliseconds()

Stamp.Mark()
for i=1 to 1000000
   X={}
   X.Append({"aaa":1})
   X.Append({"bbb":2})
   X.Append({"ccc":3})
end for
Stamp.TotalMilliseconds()

Stamp.Mark()
for i=1 to 1000000
   X={}
   X.AddReplace("aaa",1)
   X.AddReplace("bbb",2)
   X.AddReplace("ccc",3)
end for
Stamp.TotalMilliseconds()
 
Timing are 6.057 seconds, 13.815 seconds, 19.704 seconds, and 9.588 seconds respectively.
 
Nothing terribly surprising, except that multiple addreplace calls are faster than a single append (or multiple appends of course)
0 Kudos
Komag
Roku Guru

Re: Just some experimental variable timings I found interesting...

I guess I'll try to remember that "append" is a tricky vocab word, whereas "add or replace" are straightforward simple concepts, thus run faster!

0 Kudos