Constant expressions resolved at compile time?
![Phil Pilgrim (PhiPi)](https://forums.parallax.com/uploads/userpics/612/nMG83RHE7MV61.gif)
Chip,
Do expressions within Spin methods that involve constants get evaluated at compile time or runtime? ('Just wondering how much hand optimization to do.)
Thanks,
Phil
Do expressions within Spin methods that involve constants get evaluated at compile time or runtime? ('Just wondering how much hand optimization to do.)
Thanks,
Phil
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
Like Paul said, there is a CONSTANT operator which will tell the compiler to resolve a constant expression down to a single constant, rather than generate the bytecode for run-time solving. Here are examples of each way:
· x := 5 * 2 + 3··· 'Generate byte code to solve 5·* 2 + 3 at run-time
· x := constant(5 * 2 + 3)··· 'Generate constant 13, saves 6 bytes, faster
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
I guess I missed the constant operator. ('Can't wait for the paper version of the manual to come out, so I can just sit down and read through the whole thing!
'Not sure why I'd ever want constant expressions to evaluate at runtime, though, given the choice. Somewhere down the road, this would be a nice thing to have the compiler do either by default or via an option flag. The problem, though (if it matters), is that it becomes hard for the programmer to know which constant expressions get pre-evaluated and which ones get deferred. For example, 3 + 3 + x and x + (3 + 3) would probably get optimized, while x + 3 + 3 would not.
-Phil