get nanoseconds

This is a discussion on get nanoseconds within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. Im trying to make an application that it would be able to calculate the time of the execution ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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

    Performance Counters

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    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,183
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21