monitoring variable evaluation time

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 20 02:29:25 PDT 2015


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.


More information about the Digitalmars-d-learn mailing list