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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:54:18+00:00 2026-05-29T03:54:18+00:00

#include <boost/chrono.hpp> int main() { boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now(); //burn some time boost::chrono::nanoseconds ns

  • 0
#include <boost/chrono.hpp>

int main()
{ 
    boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();

    //burn some time

    boost::chrono::nanoseconds ns = boost::chrono::high_resolution_clock::now() - start;

    auto val = ns.count();
}

Under Windows 7 64 bits and boost 1.48 could I be sure that the above code will well return me a time in nanosecond/with nanosecond precision ?

EDIT: After doing some test :

  #include <boost/chrono.hpp>
    #include <windows.h>

    double PCFreq = 0.0;
    __int64 CounterStart = 0;

    void StartCounter()
    {
        LARGE_INTEGER li;
        if(!QueryPerformanceFrequency(&li))
            std::cout << "QueryPerformanceFrequency failed!\n";

        PCFreq = double(li.QuadPart)/1000.0;

        QueryPerformanceCounter(&li);
        CounterStart = li.QuadPart;
    }
    double GetCounter()
    {
        LARGE_INTEGER li;
        QueryPerformanceCounter(&li);
        return double(li.QuadPart-CounterStart)/PCFreq;
    }

    int main()
    {
        boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();
        Sleep(1000);
        boost::chrono::nanoseconds sec = boost::chrono::high_resolution_clock::now() - start;   
        std::cout << sec.count()/1000000. <<'\n';

        StartCounter();
        Sleep(1000);
        std::cout << GetCounter() <<'\n';

        std::cin.ignore(); 
    }

both method return approximatively the same result.Something strange about the above code is that first method invoqued in the main will always return the best result, switch boost in second position and GetCounter will be the best (i.e: more closer to Sleep duration)

  • 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-29T03:54:19+00:00Added an answer on May 29, 2026 at 3:54 am

    As I delve through the docs, it looks like the difference between two times returns a duration, and that high_resolution_clock might be a system_clock or a steady_clock (depending on if the OS’s API has a steady clock). steady_clock::duration is nanoseconds. system_clock docs say “The nested duration typedef has a resolution that depends on the one provided by the platform.”

    However, the duration type is able to convert between units. So sec will always be in nanoseconds, but there’s no guarantee that it will be accurate to the nanosecond that I can find.

    On the undocumented details, http://www.boost.org/doc/libs/1_48_0/boost/chrono/system_clocks.hpp shows that when BOOST_CHRONO_WINDOWS_API is defined, the resolution is 100ns, and for all other APIs resolution is 1ns. The result is in all cases stored as nanoseconds, so you don’t ever have to worry about conversions.

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

Sidebar

Related Questions

#include <iostream> #include <algorithm> #include <vector> #include <boost/array.hpp> #include <boost/bind.hpp> int main() { boost::array<int,
Very simple example: #include <string> #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc,
MinGw 4.7.0 Boost 1.47 code: #include <iostream> #include <boost/thread.hpp> int main(int argc, char *argv[])
Compiling the following: // file main.cpp #include <string> #include <boost/lexical_cast.hpp> int main() { boost::lexical_cast<std::string>(
Consider the following program: #include <iostream> #include boost/filesystem.hpp int main() { boost::filesystem::directory_entry d(test.txt); boost::filesystem::directory_entry
This is from the Boost docs and compiles without problems. #include boost/multi_array.hpp int main
I have the following code: #include <boost/shared_ptr.hpp> struct Foo { int a; }; static
What am I doing wrong here? // file main.cpp #include <iostream> #include <boost/numeric/ublas/vector.hpp> #include
The following code #include stdafx.h #include <string> #include <set> #include <boost/algorithm/string/trim.hpp> int _tmain(int argc,
I currently have this code #include <iostream> #include <curl.h> #include <windows.h> #include boost\timer.hpp int

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.