www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - monitoring variable evaluation time

reply "Clayton" <johnsjdsd gmail.com> writes:
What could be the best-tool for monitoring the evaluation time of 
a variable . What I usually do is run the command :-

- dmd -J. program.d

Then I inspect the program.o file using vi for presence of 
compile-time constants and enums. I am wondering if this is the 
right way to do this. Am a bit skeptical about this as am working 
on pushing everything that the compiler can do to the compiler 
for faster run-time.
Jul 20 2015
next sibling parent reply "anonymous" <anonymous example.com> writes:
On Monday, 20 July 2015 at 08:53:52 UTC, Clayton wrote:
 What could be the best-tool for monitoring the evaluation time 
 of a variable . What I usually do is run the command :-

 - dmd -J. program.d

 Then I inspect the program.o file using vi for presence of 
 compile-time constants and enums. I am wondering if this is the 
 right way to do this. Am a bit skeptical about this as am 
 working on pushing everything that the compiler can do to the 
 compiler for faster run-time.
You mean you want to know if some value is pre-computed during compilation? There are various places where the compiler is required to compute at compile time: * initializers of module level variables * initializers of static variables * enum values * dimensions of static arrays * template value arguments * probably a couple more I can't of right now In these places where pre-computation is guaranteed, it's called CTFE. Compilers are free to pre-compute other constants (constant folding). It's pretty safe to assume that simple, constant arithmetic expressions are evaluated at compile-time, maybe it's even guaranteed. Beyond that, things vary between compilers. Instead of checking if the compiler recognizes your constants and pre-computes them, I'd suggest to force it by using a CTFE context. To force CTFE in an otherwise non-CTFE context, you can use a little helper: ---- enum ctEval(alias expr) = expr; void main() { auto x = ctEval!(/* some complex expression that would possibly not be constant folded otherwise */); } ---- I'm not sure if this the best way to define ctEval. I thought it had found its way into phobos by now, but I can't find it anywhere. This very simple version here may have issues.
Jul 20 2015
parent "Clayton" <johnsjdsd gmail.com> writes:
On Monday, 20 July 2015 at 09:29:26 UTC, anonymous wrote:
 On Monday, 20 July 2015 at 08:53:52 UTC, Clayton wrote:
 [...]
You mean you want to know if some value is pre-computed during compilation? [...]
Thankyou a lot for the suggestion. Am noting as I learn the language.
Jul 20 2015
prev sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Monday, 20 July 2015 at 08:53:52 UTC, Clayton wrote:
 What could be the best-tool for monitoring the evaluation time 
 of a variable . What I usually do is run the command :-

 - dmd -J. program.d

 Then I inspect the program.o file using vi for presence of 
 compile-time constants and enums. I am wondering if this is the 
 right way to do this. Am a bit skeptical about this as am 
 working on pushing everything that the compiler can do to the 
 compiler for faster run-time.
I'm not sure that's what you want to know, but... The evaluation time of expressions is completely deterministic. Global (module level) or static variables, default values for struct/class members, template arguments, as well as enums, are evaluated at compile-time and thus their construction has no runtime costs. Everything else can, and usually does, happen at runtime (modulo anything I might have overlooked in the list above). But even then, compilers are free to evaluate things during optimization, as long as it's possible and makes no observable difference in behaviour. For example, with optimization enabled, both GDC and LDC can even evaluate some complex recursive function calls to their result. But these optimizations are optional. If you want a guarantee, you need to use one of the above contexts. You can use a simple helper template for that (untested): template forceCTFE(Args...) if(Args.length == 1) { alias forceCTFE = Args[0]; } void main() { auto myvar = forceCTFE!(foo(23)); }
Jul 20 2015
parent "Clayton" <johnsjdsd gmail.com> writes:
On Monday, 20 July 2015 at 10:11:10 UTC, Marc Sch├╝tz wrote:
 On Monday, 20 July 2015 at 08:53:52 UTC, Clayton wrote:
 [...]
I'm not sure that's what you want to know, but... The evaluation time of expressions is completely deterministic. Global (module level) or static variables, default values for struct/class members, template arguments, as well as enums, are evaluated at compile-time and thus their construction has no runtime costs. [...]
Thankyou a lot for the suggestion. Am noting as I learn the language.
Jul 20 2015