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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:01:10+00:00 2026-06-12T22:01:10+00:00

Possible Duplicate: C++ Timer function to provide time in nano seconds I need to

  • 0

Possible Duplicate:
C++ Timer function to provide time in nano seconds

I need to measure the duration of a function execution in nanoseconds resolution. Is it possible? Our ordinary computer hardware and software are able to give such a precision for time? If yes how to accomplish that with c++? Is it possible with Boost library?

  • 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-06-12T22:01:11+00:00Added an answer on June 12, 2026 at 10:01 pm

    Yes, today most hardware supports this sort of resolution, and the C++ standard library has an API that can support it as well. Unfortunately not all implementations of C++ actually do provide it.

    The API is the <chrono> library introduced in C++11:

    #include <iostream>
    #include <chrono>
    
    int main() {
        auto start = std::chrono::high_resolution_clock::now();
    
        // operation to be timed ...
    
        auto finish = std::chrono::high_resolution_clock::now();
        std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(finish-start).count() << "ns\n";
    }
    

    The <chrono> implementation in libc++ for Darwin provides nanosecond resolution, but it seems the implementation in VS2012 only goes to tenths of milliseconds. The above code will still give times in terms of nanoseconds, but timings less than 100 microseconds will end up being zero nanoseconds.

    Boost also provides an implemenation, boost::chrono, which does seem to use nanoseconds on Windows. It’s also usable with C++03.

    #include <boost/chrono.hpp>
    
    int main() {
        boost::chrono::high_resolution_clock::time_point t1 =
            boost::chrono::high_resolution_clock::now();
    
        boost::chrono::high_resolution_clock::time_point t2 =
            boost::chrono::high_resolution_clock::now();
    
        std::cout << boost::chrono::duration_cast<boost::chrono::nanoseconds>(t2-t1) << "\n";
        // boost chrono has more advanced output, .count() isn't needed, nor is manually writing out the units
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ Timer function to provide time in nano seconds High resolution timer
Possible Duplicate: how to provide a swap function for my class? Every time I
Possible Duplicate: how to hide a div after some time period? i need to
Possible Duplicate: fast elapsed time on linux How can I measure how long a
Possible Duplicate: How to wait for a period of time after a function run
Possible Duplicate: Does sleep time count for execution time limit? If the max execution
Possible Duplicate: C# Execute function at specific time I want to run certain function
Possible Duplicate: High resolution timer with C++ and Linux? double hires_time_in_seconds(); I'm looking for
Possible Duplicate: How to pass values to a callback function at the time of
Possible Duplicate: Linux C/C++ Timer signal handler in userspace How do i got about

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.