About the LoadString in Lua

Source: Internet
Author: User
Tags lua

f = loadstring ("i=i+1") is basically equivalent to F = function () i=i+1 end

But the second piece of code is much faster than the first, because it is compiled only once when the corresponding program block is compiled, and the first piece of code is

Each call to LoadString is compiled, because LoadString does not involve the lexical domain each time it compiles, so the above two pieces of code are not equivalent, as follows:

i = 32

Local i = 0

f = LoadString ("i = i + 1;print (i)")

g = function () i = i + 1; Print (i) end

F ()---33

G ()---1

The function g operates the local I as expected, but F operates on the global I, because loadstring always compiles its string in the global environment.

About the LoadString in Lua

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.