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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:23:44+00:00 2026-05-16T23:23:44+00:00

I do not really wish to profile because I was wanting to do many

  • 0

I do not really wish to profile because I was wanting to do many different small benchmarks on different simple functions. For the life of me I cannot find a way to record the amount of milliseconds in C++, I am using Linux by the way.

Can you suggest the method to get the system clock in milliseconds (I may settle with seconds if I cannot find an easy way..) and what header they are included in?

  • 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-16T23:23:44+00:00Added an answer on May 16, 2026 at 11:23 pm

    using gettimeofday function from sys/time.h header file, i use this class:

    #include <cstdlib>
    #include <sys/time.h>
    
    class Timer
    {
        timeval timer[2];
    
      public:
    
        timeval start()
        {
            gettimeofday(&this->timer[0], NULL);
            return this->timer[0];
        }
    
        timeval stop()
        {
            gettimeofday(&this->timer[1], NULL);
            return this->timer[1];
        }
    
        int duration() const
        {
            int secs(this->timer[1].tv_sec - this->timer[0].tv_sec);
            int usecs(this->timer[1].tv_usec - this->timer[0].tv_usec);
    
            if(usecs < 0)
            {
                --secs;
                usecs += 1000000;
            }
    
            return static_cast<int>(secs * 1000 + usecs / 1000.0 + 0.5);
        }
    };
    

    for example:

    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
        Timer tm;
        std::ostringstream ooo;
        std::string str;
    
        tm.start();
        for(int i = 0; i < 10000000; ++i)
        {
            ooo << "This is a string. ";
        }
        tm.stop();
        std::cout << "std::ostingstream -> " << tm.duration() << std::endl;
    
        tm.start();
        for(int i = 0; i < 10000000; ++i)
        {
            str += "This is a string. ";
        }
        tm.stop();
        std::cout << "std::string -> " << tm.duration() << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not really a programming question, but relevant to many programmers... Let's say I have
Not really getting the point of the map function. Can anyone explain with examples
(Not really a programming question, sorry) I'm working on benchmarking various filesystems (most importantly:
NHibernate is not really a good fit for our environment due to all the
I'm not really asking about how programmers learn how to program. More about specific
I am not really familiar with Maven program but I've been using Eclipse for
The title may not really explain what I'm really trying to get at, couldn't
So i'm not really sure why this is happening but I'm running through some
I'm not really sure how to google this one . I would like to
This is a question not really about programming (is not specific to any language

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.