ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to optimize a C++ program by counting up the elapsed time in various sections of the program. I need a function that returns time at the millisecond level or better. The program consists of a series of nested loops and I want to track the time taken up by various sections in the innermost loop (so that the sections of code I'm timing execute quickly, but the overall time adds up because of the looping).
I've been using the gettimeofday function to do this (since it gives microseconds), but it turns out to be very slow. When I measure the time elapsed using this function, it looks like most of the time is spent just fetching the current time, which doesn't help me figure out what portions of my code need optimizing. My timer function looks like this:
time_t millitime()
{
struct timeval tm;
gettimeofday(&tm,NULL);
return 1000*tm->tv_sec + tm->tv_usec/1000;
}
Does anyone know of a faster way to retrieve the current time in milliseconds?
I am trying to optimize a C++ program by counting up the elapsed time in various sections of the program. I need a function that returns time at the millisecond level or better. The program consists of a series of nested loops and I want to track the time taken up by various sections in the innermost loop (so that the sections of code I'm timing execute quickly, but the overall time adds up because of the looping).
There is a better solution for this problem: use a profiler
I was trying to avoid using a profiler since, for reasons of speed, the code avoids function calls as much as possible.
Also, I would like the flexibility of quickly checking individual statements or groups of statements for speed, or even the overhead involved with the loops themselves, without having to turn those lines into function calls for the profiler to analyze. I didn't think gprof would be convenient to use for that type of line-by-line checking.
However it may be that I am being too paranoid about how long each function call would take, and if I can't come up with a quicker timing function, it seems like gprof will be the better solution.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.