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 74609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:23:06+00:00 2026-05-10T20:23:06+00:00

I wish to calculate the time it took for an API to return a

  • 0

I wish to calculate the time it took for an API to return a value. The time taken for such an action is in the space of nanoseconds. As the API is a C++ class/function, I am using the timer.h to calculate the same:

  #include <ctime>   #include <iostream>    using namespace std;    int main(int argc, char** argv) {        clock_t start;       double diff;       start = clock();       diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;       cout<<"printf: "<< diff <<'\n';        return 0;   } 

The above code gives the time in seconds. How do I get the same in nano seconds and with more precision?

  • 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. 2026-05-10T20:23:07+00:00Added an answer on May 10, 2026 at 8:23 pm

    What others have posted about running the function repeatedly in a loop is correct.

    For Linux (and BSD) you want to use clock_gettime().

    #include <sys/time.h>  int main() {    timespec ts;    // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD    clock_gettime(CLOCK_REALTIME, &ts); // Works on Linux } 

    For windows you want to use the QueryPerformanceCounter. And here is more on QPC

    Apparently there is a known issue with QPC on some chipsets, so you may want to make sure you do not have those chipset. Additionally some dual core AMDs may also cause a problem. See the second post by sebbbi, where he states:

    QueryPerformanceCounter() and QueryPerformanceFrequency() offer a bit better resolution, but have different issues. For example in Windows XP, all AMD Athlon X2 dual core CPUs return the PC of either of the cores ‘randomly’ (the PC sometimes jumps a bit backwards), unless you specially install AMD dual core driver package to fix the issue. We haven’t noticed any other dual+ core CPUs having similar issues (p4 dual, p4 ht, core2 dual, core2 quad, phenom quad).

    EDIT 2013/07/16:

    It looks like there is some controversy on the efficacy of QPC under certain circumstances as stated in http://msdn.microsoft.com/en-us/library/windows/desktop/ee417693(v=vs.85).aspx

    …While QueryPerformanceCounter and QueryPerformanceFrequency typically adjust for multiple processors, bugs in the BIOS or drivers may result in these routines returning different values as the thread moves from one processor to another…

    However this StackOverflow answer https://stackoverflow.com/a/4588605/34329 states that QPC should work fine on any MS OS after Win XP service pack 2.

    This article shows that Windows 7 can determine if the processor(s) have an invariant TSC and falls back to an external timer if they don’t. http://performancebydesign.blogspot.com/2012/03/high-resolution-clocks-and-timers-for.html Synchronizing across processors is still an issue.

    Other fine reading related to timers:

    • https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks
    • http://lwn.net/Articles/209101/
    • http://performancebydesign.blogspot.com/2012/03/high-resolution-clocks-and-timers-for.html
    • QueryPerformanceCounter Status?

    See the comments for more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer There are a couple software solutions out there that can… May 11, 2026 at 2:58 pm
  • added an answer This will certainly be easier with a RISC-ish fixed-length instruction… May 11, 2026 at 2:58 pm
  • added an answer You can't. And frankly I don't know of any supported… May 11, 2026 at 2:58 pm

Related Questions

I have a file with the following values: for 3 threads: Average time taken
I'm writing a program that implements the Boneh-Franklin Identity Based Encryption. For the actual
How to build a simple recommendation system? I have seen some algorithms but it
I wish to know all the pros and cons about using these two methods.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.