lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I may be wrong, but I think what Roberto was saying was, dostring is an
expensive operation because it has to compile the string before it can execute
it. Thus it's better for you to just query the global with one of the available
functions, rather than doing it in a way that has to compile the function every
time you do it. In that regard, dofile would also be expensive, unless you were
dofile'ing code that was precompiled with luac or something. Even then, you have
the overhead of loading in the code before it gets run.

As for require, I may not be 100%, but I belive it does the same thing as
dofile, except that it has a path that it will look for files in, and it won't
bother doing the same file a second time. So whereas

dofile ("bob.lua") dofile ("bob.lua")

will load, parse, and execute the file bob.lua twice,

require ("bob.lua") require ("bob.lua")

will only do it once. [1]

I would suspect that it is a bit slower than a regular dofile because it's doing
extra checks to see if it should even bother doing a file. I doubt the extra
penalty is large enough to be of any concern though. Subsequent calls with the
same filename would be faster, however, since once it determined that the file
was already loaded, it wouldn't do anything.

As for, should you write your own? If that's the sort of functionality you need,
it might be a good idea. Probably you could get away with "require = dofile" in
a pinch,and remove that when you upgrade.

And for storing a flag, you probably just want to test for nil/non-nil using a
number

[1]

I'm not entirely sure if require takes only one parameter or more than one. I
haven't actually used the new version yet. But I think it's like a "drop in
replacement" for dofile. 
======================
On Thu, 10 Jan 2002 21:57:09 +0100 (GMT)
Markus Huber <pulse@evolution.org> wrote:

> > Roberto wrote:
> 
> > As a rule, you should always avoid using dostring if you can (it is
> > too expensive).
> 
> Is dofile() also so expensive? But I see no other way to load
> code from external files (require() is not implemented in the version
> 4.0 and 4.1 is in beta state). Is require() faster as dofile()? What is
> the difference between both? Should I write a pseudo require() and use
> this until the 4.1 is ready to use?
> 
> Again thank you very much for your help. There are many discussions
> about Lua and Robertos Lua book is fantastic but often its not easy to
> find the definitive answers.
> 
> What methods are you suggests for variables that only hold an flag?
> Which flag values? true/false nil/not nil...?
> 
> 
> --
> Markus
>