Thread: get nanoseconds

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    12

    get nanoseconds

    Hello everyone.
    Im trying to make an application that it would be able to calculate the time of the execution in nanoseconds.
    After using google for a while i found a lot of suggestions which most of them dont actually work(most of them are in miliseconds - at least what i found till now).
    Can someone provide me a simple example of how i can do that, if it is possible, or a web page or article to read so i can do what i have to do.
    Thank you

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Use the clock() function from <ctime>.
    And divide the difference in the number of ticks you find by CLOCKS_PER_SEC * 1e9 .

    If you can use C++11 features, <chrono> has a great High Resolution timer....giving you the best of what your Operating System is capable of.

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    if you're on windows there's performance counter

    Performance Counters

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by kspen View Post
    Hello everyone.
    Im trying to make an application that it would be able to calculate the time of the execution in nanoseconds.
    After using google for a while i found a lot of suggestions which most of them dont actually work(most of them are in miliseconds - at least what i found till now).
    Can someone provide me a simple example of how i can do that, if it is possible, or a web page or article to read so i can do what i have to do.
    Thank you
    this won't actually give you execution time down to nanoseconds. if the original measurement doesn't have that level of resolution, you can't replace it mathematically. if the information isn't there to start with, it never will be.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    If your CPU runs at 1GHz, one clock cycle is one nanosecond. That means, you'll need something that basically counts CPU clock cycles, instead of an actual "timer". I am not aware of a way to do that on Windows/Linux.

    Getting accurate microsecond precision is fairly difficult already.

    Why do you need ns?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Time Stamp Counter - Wikipedia, the free encyclopedia
    Make sure you understand all the caveats, such as multiple cores, variable clock speed and so on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    12
    thank you for your answers guys!

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Make sure you heed what Salem posted when doing this. There are all kinds of gotchas when using this in Windows based on the current model of CPU the system is using. I can't tell you how many times I've been bitten by this. Most modern quad-core processors will be fine but you will run into issues on earlier processors and pre-Vista OS's. You may even run into this outside of Windows although I have no experience to back that up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count execution time in ubuntu(nanoseconds)
    By nik in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2011, 07:20 PM