Menu
Home
Create new Paste
Log in
Code
Theme: cobalt
Theme: eclipse
Theme: elegant
Theme: monokai
Theme: neat
Theme: night
Theme: rubyblue
module main; import std.stdio; import std.datetime; template nameOf(alias a) { enum string nameOf = __traits(identifier, a); } template qualifiedName(alias a) { // does it have a parent? static if (__traits(compiles, __traits(parent, a))) // If yes, get the name and recurse enum qualifiedName = qualifiedName!(__traits(parent, a)) ~ "." ~ nameOf!(a); // if not, it’s a module name. Stop there. else enum qualifiedName = nameOf!a; } mixin template getScopeName() { enum scopeName = qualifiedName!(__traits(parent, scopeName)); } string timeIt() { return q{ version(TimeIt) { mixin getScopeName; auto sw = StopWatch(AutoStart.yes); scope(exit) { sw.stop(); writefln("%s done in %s msecs.", scopeName, sw.peek.msecs); } } }; } void test() { mixin(timeIt); int[] a; foreach (i; 0 .. 1_000_000) a ~= int.init; } version = TimeIt; void main() { test(); }
Result:
Success
/
Return code: 0
/
Compilation time:
0.502
seconds
/
Run time:
0.072
seconds
Application output:
main.test done in 70 msecs.
Username
Message
Add comment
Paste info
Author:
Guest
Views:
128
Private:
no
Expires:
Never
Uploaded:
17.09.12 20:14
Votes
:
0
Tweet
Compilation
Compiler:
DMD 2.060
Pointer size:
m32
Actions
Download
Fork
Raw
×
Confirm
Are you sure you want to delete this paste?
There's no way back!
×
Confirm
Reason