Logo

Programming-Idioms

  • C#
  • Clojure

Idiom #125 Measure function call duration

measure the duration t, in nanoseconds, of a call to the function foo. Print this duration.

(time (foo))

time prints time elapsed, but in ms
using System.Diagnostics;
var stopwatch = new Stopwatch();
stopwatch.Start();
foo();
stopwatch.Stop();
var t = stopwatch.ElapsedMilliseconds;
#include <chrono>
auto start = std::chrono::steady_clock::now();
foo();
auto end = std::chrono::steady_clock::now();
auto t = std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count();

c++ 11

New implementation...
< >
JPSII