From 8800367aed16d3c1fb0842343e3a3ca99e3e333c Mon Sep 17 00:00:00 2001 From: ketmar Date: Tue, 26 Aug 2014 08:44:29 +0300 Subject: [PATCH] added "--time" and "--compile-time" options --- rgdc.d | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/rgdc.d b/rgdc.d index c9a778a..c677f49 100644 --- a/rgdc.d +++ b/rgdc.d @@ -46,6 +46,8 @@ private string[] compilerExtraFlags = ["-pipe"]; private bool optDynamic = false; private bool optShowCommands = false; +private bool optRunTiming = false; +private bool optCompileTiming = false; //////////////////////////////////////////////////////////////////////////////// @@ -306,12 +308,16 @@ private int rebuild (string root, string fullExe, } if (commandLength+compilerBinary.length+compilerExtraFlags.join(" ").length+eflags2.join(" ").length+ prlibs.join(" ").length > 32700) throw new Exception("SHIT!"); + Timer btm; + btm.start(); immutable result = run([compilerBinary]~compilerExtraFlags~eflags2~todo~prlibs); + btm.stop(); if (result) { // build failed if (exists(fullExeTemp)) remove(fullExeTemp); return result; } + if (!dryRun && optCompileTiming) writeln("RGDC: compile time: ", btm); // clean up the dir containing the object file, just not in dry // run mode because we haven't created any! if (!dryRun) { @@ -537,6 +543,8 @@ addition to compiler options, rgdc recognizes the following options: --help this message --main add a stub main program to the mix (e.g. for unittesting) --shebang rgdc is in a shebang line (put as first argument) + --time measure program executing time + --compile-time measure compile time "; } @@ -595,6 +603,8 @@ int main(string[] args) { string[string] simpleReplace; simpleReplace["-unittest"] = "-funittest"; simpleReplace["-main"] = "--main"; + simpleReplace["-time"] = "--time"; + simpleReplace["-compile-time"] = "--compile-time"; simpleReplace["-O"] = "-O2"; simpleReplace["-boundscheck=off"] = "-fno-bounds-check"; simpleReplace["-boundscheck=on"] = "-fbounds-check"; @@ -663,8 +673,10 @@ int main(string[] args) { "help", { writeln(helpString); bailout = true; }, "main", &addStubMain, "show-commands", &optShowCommands, - "dynamic", { optDynamic = true; }, + "dynamic", &optDynamic, "static", { optDynamic = false; }, + "time", &optRunTiming, + "compile-time", &optCompileTiming ); if (bailout) return 0; if (dryRun) chatty = true; // dry-run implies chatty @@ -777,5 +789,79 @@ int main(string[] args) { unlockWorkPath(); // run + if (!dryRun && optRunTiming) { + Timer rtm; + rtm.start(); + int res = run(exe~programArgs); + rtm.stop(); + writeln("RGDC: execution time: ", rtm); + return res; + } return exec(exe~programArgs); } + + +//////////////////////////////////////////////////////////////////////////////// +struct Timer { + import core.time; + + enum State { + Stopped, + Running, + Paused + } + + @property auto state () @safe const nothrow { return mState; } + + @property bool stopped () @safe const nothrow { return (mState == State.Stopped); } + @property bool running () @safe const nothrow { return (mState == State.Running); } + @property bool paused () @safe const nothrow { return (mState == State.Paused); } + + void reset () @trusted nothrow { + mAccum = Duration.zero; + mSTime = MonoTime.currTime; + } + + void start () @trusted { + if (mState != State.Stopped) throw new Exception("Timer.start(): invalid timer state"); + mAccum = Duration.zero; + mState = State.Running; + mSTime = MonoTime.currTime; + } + + void stop () @trusted { + if (mState != State.Running) throw new Exception("Timer.stop(): invalid timer state"); + mAccum += MonoTime.currTime-mSTime; + mState = State.Stopped; + } + + void pause () @trusted { + if (mState != State.Running) throw new Exception("Timer.pause(): invalid timer state"); + mAccum += MonoTime.currTime-mSTime; + mState = State.Paused; + } + + void resume () @trusted { + if (mState != State.Paused) throw new Exception("Timer.resume(): invalid timer state"); + mState = State.Running; + mSTime = MonoTime.currTime; + } + + string toString () @trusted const { + import std.string; + Duration d; + final switch (mState) { + case State.Stopped: case State.Paused: d = mAccum; break; + case State.Running: d = mAccum+(MonoTime.currTime-mSTime); break; + } + auto tm = d.split!("hours", "minutes", "seconds", "msecs")(); + if (tm.hours) return format("%s:%02d:%02d.%03d", tm.hours, tm.minutes, tm.seconds, tm.msecs); + if (tm.minutes) return format("%s:%02d.%03d", tm.minutes, tm.seconds, tm.msecs); + return format("%s.%03d", tm.seconds, tm.msecs); + } + +private: + State mState = State.Stopped; + MonoTime mSTime; + Duration mAccum; +} -- 2.11.4.GIT