From b818799800520bcba5538b03da727d135bd43664 Mon Sep 17 00:00:00 2001 From: Glen Fraser Date: Mon, 4 May 2015 19:40:57 +0200 Subject: [PATCH 1/2] Added getTimeInSeconds() helper to chai executable --- src/main.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 6dcfa2b..fcb41d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -249,6 +249,15 @@ void interactive(chaiscript::ChaiScript& chai) } } +double getTimeInSeconds() +{ + using namespace std::chrono; + static time_point start = high_resolution_clock::now(); + + duration elapsed_seconds = high_resolution_clock::now() - start; + return elapsed_seconds.count(); +} + int main(int argc, char *argv[]) { @@ -288,6 +297,8 @@ int main(int argc, char *argv[]) chai.add(chaiscript::fun(&help), "help"); chai.add(chaiscript::fun(&throws_exception), "throws_exception"); chai.add(chaiscript::fun(&get_eval_error), "get_eval_error"); + chai.add(chaiscript::fun(&getTimeInSeconds), "getTimeInSeconds"); + for (int i = 0; i < argc; ++i) { if ( i == 0 && argc > 1 ) { From bd176cfde2ba66505771c37c88fcf39a89b2b4a3 Mon Sep 17 00:00:00 2001 From: Glen Fraser Date: Wed, 6 May 2015 11:37:37 +0200 Subject: [PATCH 2/2] Renamed timer function to now(), added perf unit test --- src/main.cpp | 10 ++++------ unittests/performance.chai | 13 +++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 unittests/performance.chai diff --git a/src/main.cpp b/src/main.cpp index fcb41d5..3a72df1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -249,13 +249,11 @@ void interactive(chaiscript::ChaiScript& chai) } } -double getTimeInSeconds() +double now() { using namespace std::chrono; - static time_point start = high_resolution_clock::now(); - - duration elapsed_seconds = high_resolution_clock::now() - start; - return elapsed_seconds.count(); + auto now = high_resolution_clock::now(); + return duration_cast>(now.time_since_epoch()).count(); } int main(int argc, char *argv[]) @@ -297,7 +295,7 @@ int main(int argc, char *argv[]) chai.add(chaiscript::fun(&help), "help"); chai.add(chaiscript::fun(&throws_exception), "throws_exception"); chai.add(chaiscript::fun(&get_eval_error), "get_eval_error"); - chai.add(chaiscript::fun(&getTimeInSeconds), "getTimeInSeconds"); + chai.add(chaiscript::fun(&now), "now"); for (int i = 0; i < argc; ++i) { diff --git a/unittests/performance.chai b/unittests/performance.chai new file mode 100644 index 0000000..1345c74 --- /dev/null +++ b/unittests/performance.chai @@ -0,0 +1,13 @@ +var sum = 0.0 +var start = now() +for (var i = 1; i <= 100000; ++i) { + if (i % 2 == 0) { + sum += 1.0 / i; + } + else { + sum += 1.0 / (double(i) * i); + } +} +var end = now() +print("Elapsed time: " + to_string(end - start) + " sum: " + to_string(sum)) +assert_equal(to_string(sum), "6.9322")