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 168647

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:29:25+00:00 2026-05-11T12:29:25+00:00

I want to measure the speed of a function within a loop. But why

  • 0

I want to measure the speed of a function within a loop. But why my way of doing it always print ‘0’ instead of high-res timing with 9 digits decimal precision (i.e. in nano/micro seconds)?

What’s the correct way to do it?

#include <iomanip> #include <iostream> #include <time.h> int main() {    for (int i = 0; i <100; i++) {     std::clock_t startTime = std::clock();      // a very fast function in the middle     cout << 'Time: ' << setprecision(9) << (clock() - startTime + 0.00)/CLOCKS_PER_SEC << endl;  }   return 0; } 

Related Questions:

  • How to overcome clock()‘s low resolution
  • High Resolution Timer with C++ and linux
  • Equivalent of Windows’ QueryPerformanceCounter on OSX
  • 0 0 Answers
  • 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-11T12:29:26+00:00Added an answer on May 11, 2026 at 12:29 pm

    Move your time calculation functions outside for () { .. } statement then devide total execution time by the number of operations in your testing loop.

    #include <iostream> #include <ctime> #define NUMBER 10000 // the number of operations  // get the difference between start and end time and devide by // the number of operations double diffclock(clock_t clock1, clock_t clock2) {     double diffticks = clock1 - clock2;     double diffms = (diffticks) / (CLOCKS_PER_SEC / NUMBER);     return diffms; }  int main() {     // start a timer here     clock_t begin = clock();      // execute your functions several times (at least 10'000)     for (int i = 0; i < NUMBER; i++) {         // a very fast function in the middle         func()     }      // stop timer here     clock_t end = clock();      // display results here     cout << 'Execution time: ' << diffclock(end, begin) << ' ms.' << endl;     return 0; } 

    Note: std::clock() lacks sufficient precision for profiling. Reference.

    • 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 You might want to try buddy classloading. For more info… May 11, 2026 at 2:57 pm
  • added an answer For V1, I would wrap the switch statement in a… May 11, 2026 at 2:57 pm
  • added an answer yourNumber.ToString('N2'); May 11, 2026 at 2:57 pm

Related Questions

Right now we just use something like this stopWatch.Start(); try { method(); } finally
I want to know which tool can be used to measure the cyclomatic complexity
I need to measure the time it takes for a function to complete in
i've been trying to find a method in C# to measure the size of

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.