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));
}