Quoted or unquoted literals

J. Landman Gay jacque at hyperactivesw.com
Mon May 11 14:38:24 EDT 2009


I was curious about the time difference between quoted and unquoted 
literals, which we were discussing in another thread. Though our tests 
seemed to show there is no significant difference, conceptually it 
didn't make any sense to me. So I asked one of the engineers at RR about 
it and got some interesting info.

As Jerry J suggested, it's true that any unquoted literal is resolved at 
compile time, and after that it acts just as a quoted literal would. A 
temporary variable is created and given the value of its own name if you 
don't supply a different value, so myFld becomes a variable named 
"myFld" with a value of "myFld". A compile usually happens every time 
the script is run for the first time during a session. In real world 
applications, the difference in speed would be unnoticeable because the 
resolution only has to happen once. These would both take about the same 
amount of time after the first usage:

get fld testFld
get fld "testFld"

Our original tests, which ran a repeat loop inside a single handler, 
would only have to resolve the field name once, and after that all 
references to the field behaved as though they were a quoted field name 
-- which in essence, they were.

But there is a case where the engine recompiles the script on every 
access, and that is when using the "send" command. The engine re-parses 
the script on each call before it handles the message. So a better test 
would be to run a repeat loop that sends a command to another object. We 
set up a test to do that:

# Btn 1 script:
on mouseUp
    put the milliseconds into start
    repeat 100000
       send "test" to btn 2
    end repeat
    put the milliseconds - start into tTest1
    put the milliseconds into start
    repeat 100000
       send "test" to btn 3
    end repeat
    put the milliseconds - start into tTest2
    put tTest1 && tTest2
end mouseUp

# Btn 2 script (quoted field name):
on test
    get the text of fld "testFld"
end test

# Btn 3 script (unquoted field name):
on test
   get the text of fld testFld
end test

The difference in speed is apparent using this benchmark. The engine has 
to reparse the field name on every call. On the RR machine the 
difference was about 20ms. On my slower machine, it was 54ms.

In real-world situations, there probably isn't much difference between 
quoted and unquoted literals, unless you are sending thousands of 
requests to other objects. If your scripts are only using an unquoted 
literal locally, it will be resolved the first time the engine sees it 
and behave just like a quoted literal for the remainder of that 
particular script.

I repeated the test to see if including "the" when refering to 
properties made a difference. In buttons 2 and 3:

get rect of fld "testFld"
get the rect of fld "testFld"

In this test only the word "the" varies. The difference in speed when 
omitting "the" from property references is much smaller than when 
dealing with unquoted literals, but it still exists. In real-world usage 
it probably doesn't matter much.

Geek trivia. :)

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com



More information about the use-livecode mailing list