A trivial question about execution speed

5 years 10 months ago #29533031 by LROBBINS
Suppose I have to frequently, in every loop cycle, use a value stored in a Roboteq variable, for example _ALIM,1. Will script execution be faster if I do
IF ILeft > GetValue (_ALIM,1) THEN  ... ' in every loop cycle

or

if I assign that number to a variable within the script and use that variable each time I need the number, for example:
MaxAmps1 = GetValue (_ALIM,1) ' just once
IF  ILeft > MaxAmps1 THEN ...  ' in every loop cycle

Ciao,
Lenny

Please Log in or Create an account to join the conversation.

5 years 10 months ago #29533034 by blake
Hi Lenny,

Technically speaking when querying and reading it will be faster (although in most cases nominally) if you use:
IF ILeft > GetValue (_ALIM,1) THEN  ... ' in every loop cycle

Although, this is only true of the data you are querying is a changing variable. In this example specifically, _ALIM is a configuration parameter for your amps limit. Does this setting ever change this configuration setting for any reason in your script? If not then it would definitely be easier and faster to use the second case:
MaxAmps1 = GetValue (_ALIM,1) ' just once
IF  ILeft > MaxAmps1 THEN ...  ' in every loop cycle

You would then simply define the MaxAmps1 at the beginning of your script on the outside of your loop.

Please Log in or Create an account to join the conversation.

5 years 10 months ago #29533035 by LROBBINS
Hi Blake,

I wasn't clear enough in my question and I also wrote GetValue when I should have written GetConfig. _ALIM is not changing; if it were there'd be no choice except using GetValue, and yes, doing GetValue once and then doing an assignment repeatedly is faster - but insignificantly so.

While awaiting a reply I wrote a little script to test this. Here are the results:

!r
+
exec time
1e6 loop cycles, 2 GetConfig or assignments/loop cycle -- no motor commands
time for repeated GetConfig = 17
time for repeated assign = 15
!r
+
exec time
5e5 loop cycles, 2 GetConfig or assignments/loop cycle -- 4 motor commands/cycle
time for repeated GetConfig = 15
time for repeated assign = 14
!r
+
exec time
time for repeated GetConfig = 15
time for repeated assign = 14


Hope what I did is clear enough with just this output.

So, even though the script only gets a slice of processor every msec, when it does have the processor's attention it is probably running at clock speed. That's really what I was trying to figure out.

Ciao,
Lenny

Please Log in or Create an account to join the conversation.

Time to create page: 0.067 seconds