LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2008, 09:42 PM   #1
newbix
Member
 
Registered: Oct 2002
Distribution: RedHat 9
Posts: 43

Rep: Reputation: 15
quickest way to fetch time in C++


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?

Many thanks in advance.
 
Old 04-01-2008, 10:06 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Have you tried the clock() function?
 
Old 04-02-2008, 02:12 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by newbix View Post
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
 
Old 04-02-2008, 07:51 AM   #4
newbix
Member
 
Registered: Oct 2002
Distribution: RedHat 9
Posts: 43

Original Poster
Rep: Reputation: 15
Thanks Dan04:

I will try the clock() function today.

Thanks Hko:

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.

Thank you...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Quickest way to take a scrshot and crop 'n'save hamster Linux - Software 8 11-13-2007 07:06 AM
quickest-loading distro? newbiesforever Linux - General 7 02-18-2007 04:38 PM
The quickest of make questions gauge73 Linux - Newbie 3 02-19-2004 10:42 AM
Quickest way to end Mandrake 9.2 thorman82 Mandriva 3 11-29-2003 10:32 PM
quickest way to upgrade Gnome? asianboi2k Linux - Software 3 10-02-2003 11:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration