1

I am trying to create a timer to profile some functions. For example,

#include <date_time/posix_time/posix_time.hpp>

boost::posix_time::ptime t1, t2;
t1 = boost::posix_time::microsec_clock::local_time();
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
t2 = boost::posix_time::microsec_clock::local_time();

std::cout << to_simple_string(t1) << std::endl;
std::cout << to_simple_string(t2) << std::endl;

The output I get is this,

2012-Mar-04 08:21:24.760019
2012-Mar-04 08:21:25.760577

The actual time elapsed is 1 second and 558 micro second, is there anyway that I can improve this accuracy to say under 1 microsecond?

I also tried something like this, with linker -lrt.

#include <time.h>
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &timer_start);
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
clock_gettime(1CLOCK_PROCESS_CPUTIME_ID, &timer_end);

But the compiler returns a semantic error for CLOCK_PROCESS_CPUTIME_ID. The error message is as follow.

Description Resource    Path    Location    Type
Symbol 'CLOCK_PROCESS_CPUTIME_ID' could not be resolved _OVERVIEW.cpp   /FUTETP/BACKEND line 32 Semantic Error
Mat
  • 202,337
  • 40
  • 393
  • 406
2607
  • 4,037
  • 13
  • 49
  • 64
  • Could you please show how you're compiling? (Full compiler command line) And what OS are you using? – Mat Mar 04 '12 at 14:42
  • 3
    You seem to be assuming that the it's the timer call that is inaccurate. There's probably a much larger inaccuracy in the `sleep` call. – interjay Mar 04 '12 at 15:04
  • @Mat, I am using eclipse on Fedora 15, gnu gcc 4.6. – 2607 Mar 04 '12 at 15:08
  • @mat, make all Building file: ../BACKEND/_OVERVIEW.cpp Invoking: GCC C++ Compiler g++ -std=c++0x `wx-config --static=no --cxxflags` -I/usr/local/lib/wx/include/gtk2-ansi-release-2.8 -I/usr/local/include/wx-2.8 -I/usr/local/include/boost -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"BACKEND/_OVERVIEW.d" -MT"BACKEND/_OVERVIEW.d" -o "BACKEND/_OVERVIEW.o" "../BACKEND/_OVERVIEW.cpp" Finished building: ../BACKEND/_OVERVIEW.cpp – 2607 Mar 04 '12 at 15:08
  • Linker. Building target: FUTETP Invoking: GCC C++ Linker g++ -L/usr/local/lib `wx-config --static=no --libs` -Wl,--rpath,/usr/local/lib/ -o "FUTETP" ./src/FUTETP.o ./GUI/MyFrame.o ./GUI/MyPanelInst.o ./GUI/MyPanelOverview.o ./GUI/MyPanelSymbol.o ./GUI/MyTextCtrl.o ./GUI/MyToggleButton.o ./GUI/_BASE_GUI.o ./BACKEND/MarketDataInterface.o ./BACKEND/OrderInterface.o ./BACKEND/_BASE.o ./BACKEND/_INSTRUMENT.o ./BACKEND/_OVERVIEW.o ./BACKEND/_SYMBOL.o -lrt -lboost_thread -lboost_date_time Finished building target: FUTETP – 2607 Mar 04 '12 at 15:08
  • Can't repro the build failure with GCC 4.6.2/4.7.smthing/4.5.3 (after removing the obvious typo in the second `clock_gettime` call anyway). – Mat Mar 04 '12 at 15:18
  • @Mat, thank you for testing the code. I think the problem is that the compiler is unable to recognize CLOCK_PROCESS_CPUTIME_ID. – 2607 Mar 04 '12 at 15:45
  • @2607 I have the same semantic error, although the program seems working. Have you find a solution? – elect Jul 08 '12 at 12:02

1 Answers1

1

Please keep in mind two things:

  1. sleep() and other time-sensitive commands could be inaccurate for several reasons (system load, etc.). You will never get within 1 microsecond accuracy. 500 microseconds isn't too bad for timing sleep()
  2. If your system time is controlled by NTP, etc. and the clock is changed during your timing operation, you will see this. If the time is rolled back, it is possible to obtain a negative (or large unsigned number).

By the way, I'm unsure of what method Boost is using to get the time, but consider gettimeofday(2) from sys/time.h (or newer clock_gettime() from time.h). It returns the time since midnight Jan 1, 1970. Record the time before and after and subtract. Here's an example of gettimeofday().

#include <sys/time.h>

#include <stdio.h>
#include <unistd.h>

unsigned int
timeDif(
    struct timeval *before,
    struct timeval *after)
{
    return ((after->tv_sec * 1000000 + after->tv_usec) - 
        (before->tv_sec * 1000000 + before->tv_usec));
}

int
main(
    int argc,
    char *argv[]) {
    struct timeval before, after;

    gettimeofday(&before, NULL);
    sleep(1);
    gettimeofday(&after, NULL);

    printf("Elapsed time: %u microseconds\n", timeDif(&before, &after));

    return (0);
}
greg
  • 4,843
  • 32
  • 47
  • thank you for your reply. I didn't know it is difficult to get a high accuracy on timing functions such as sleep(). I used boost::posix_time before and got an accuracy error around 500msec, that's why I switched to time.h. – 2607 Mar 04 '12 at 22:44