stopwatch


The os_stopwatch class supports traditional stopwatch facilities and is useful for timing operations and general benchmarking. Although a stopwatch stores start and stop times down to microsecond levels, the inherent delays due to operating system paging and context switching tends to make stopwatches accurate to only +/- 1 second.

Example <ospace/time/examples /stopwch1.cpp>
#include <iostream>
#include <unistd.h> // UNIX version of example
#include <ospace/time.h>

void
main()
  {
  os_time_toolkit initialize;

  os_stopwatch stopwatch;
  cout << "stopwatch.running() = " << stopwatch.running() << endl;
  cout << "stopwatch.lap() = " << stopwatch.lap() << endl;
  cout << "start and sleep for 1 second..." << endl;
  stopwatch.start();
  ::sleep( 1 );
  cout << "stopwatch.lap() = " << stopwatch.lap() << endl;
  cout << "sleep for 1 second..." << endl;
  ::sleep( 1 );
  cout << "stop" << endl;
  stopwatch.stop();
  cout << "stopwatch.lap() = " << stopwatch.lap() << endl;
  cout << "sleep for 1 second..." << endl;
  ::sleep( 1 );
  cout << "stopwatch.lap() = " << stopwatch.lap() << endl;
  cout << "start and sleep for 1 second..." << endl;
  stopwatch.start();
  ::sleep( 1 );
  cout << "stopwatch.lap() = " << stopwatch.lap() << endl;
  cout << "reset, start, and sleep for 1 second..." << endl;
  stopwatch.reset ();
  stopwatch.start ();
  ::sleep( 1 );
  cout << "stopwatch.lap() = " << stopwatch.lap() << endl;
  }

stopwatch.running() = 0
stopwatch.lap() = 0 seconds
start and sleep for 1 second...
stopwatch.lap() = 1 second
sleep for 1 second...
stop
stopwatch.lap() = 2 seconds
sleep for 1 second...
stopwatch.lap() = 2 seconds
start and sleep for 1 second...
stopwatch.lap() = 3 seconds
reset, start, and sleep for 1 second...
stopwatch.lap() = 1 second

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.