Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7599213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:36:24+00:00 2026-05-30T22:36:24+00:00

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

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:36:25+00:00Added an answer on May 30, 2026 at 10:36 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create core data application. Some times when trying to save data,
I have been trying to create a timer program with VB 2010 to the
I am trying to create a timer job in WSS 3.0. My timer job
I'm trying to create a countdown timer that takes countdown, an IBOutlet connected to
I'm trying to create recurring payment profile using CreateRecurringPaymentsProfile method of Paypal API version
I'm trying to create a new div each time a text field contained in
I'm trying to create an instance of a class at run time. The classes
I'm trying to create a method that will sum two timeO objects and return
I'm trying to dynamically create a connection string during compile time: private static string
I'm trying to use the Timespan class to create a start time and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.