The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Kshell scripts and timing dbridle AIX 3 10-10-2006 01:26 PM
Timing out a SSH rcunn87 Shell Programming and Scripting 9 07-31-2006 06:11 PM
scp timing out jph UNIX for Advanced & Expert Users 1 06-09-2005 11:59 PM
bourne shell timing question gillbates UNIX for Dummies Questions & Answers 7 02-01-2004 04:44 PM
timing a loop in unix. wolkott Shell Programming and Scripting 1 02-04-2003 02:52 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-29-2006
bebop1111116 bebop1111116 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 30
timing your functions

hi everyone. If you have a function created in your code and you want to find out how long it takes for it to run you can use a struct called gettimeofday().

so lets say we have a function like this
int myfunction (int r)
{
/*some math calculations*/
return answer;
}

How do i set up gettimeofday to find out how long it takes for the function to run. thanks
  #2 (permalink)  
Old 10-30-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,763
Suggestion: use a profiler. Most UNIX systems compilers come with one.
Code:
cc myfile.c -o myfile -g -p
./myfile # run your code #
prof ./myfile
You will get a display of how long your code spent in every function, plus the number of times it was called. The reason for the suggestion - it doesn't require instrumenting your code with timing calls.
  #3 (permalink)  
Old 10-30-2006
odys odys is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 59
Simplest way to obtain timing info if to run:
time ./a.out

For info within program Id suggest using clock() instead of gettimeofday().

#include <time.h>
clock_t t0, t1;
t0=clock();
fun();
t1=clock();
printf("%f",(t1-t0)/CLOCKS_PER_SEC);

Regards.
  #4 (permalink)  
Old 11-01-2006
DreamWarrior DreamWarrior is offline
Registered User
  
 

Join Date: Oct 2003
Posts: 70
To use gettimeofday you can do this:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static float getTimeDiff(struct timeval *s, struct timeval *e);

void main()
{
    struct timeval  sTv, eTv;

    gettimeofday(&sTv, NULL);

    /* do work */

    gettimeofday(&eTv, NULL);

    printf("Time difference is %.2f seconds\n", getTimeDiff(&sTv, &eTv));
}
The getTimeDiff function works on the two timeval structures to compute the seconds elapsed. The source is simple, and as follows:

Code:
static float getTimeDiff(struct timeval *s, struct timeval *e)
{
	struct timeval	diff_tv;
		
	diff_tv.tv_usec = e->tv_usec - s->tv_usec;
	diff_tv.tv_sec = e->tv_sec - s->tv_sec;
		
	if (s->tv_usec > e->tv_usec)
	{
		diff_tv.tv_usec += 1000000;
		diff_tv.tv_sec--;
	}
		
	return (float) diff_tv.tv_sec + ((float) diff_tv.tv_usec / 1000000.0);
}
Furthermore, it should be noted that clock() does *NOT* return values based upon real time, rather it returns CPU time. If you want to know how much REAL TIME something takes then clock() is not the solution. For example:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void main()
{
	int	i, j, t;
	
	clock_t	clockSample[3];

	clockSample[0] = clock();
	
	sleep(2);
		
	clockSample[1] = clock();
	
	for (i = 0; i < 1024; i++)
		for (j = 0; j < 1024; j++)
			t = i * i + j;
	
	clockSample[2] = clock();
	
	for (i = 0; i < 3; i++)
	{
		printf("Clock sample %d is %lu\n", i, clockSample[i]);
	}
}
Upon running this code, you'll notice that the first two samples for clock are positioned squarely at 0. The sleep call, which will take around 2 seconds *real* time, has no effect on the second clock sample. Since sleep takes no CPU time, it is not calculated into the real time usage of the application by clock(). Similar behavior will be seen for other non-CPU (blocking) tasks the application incurs, such as waiting in a select call for socket I/O. The third clock sample, however, does incur CPU time as it is a CPU bound loop. Thus, the third sample will reflect the CPU time incurred by the loop. If, however, you had the crazy idea to insert (for example) a usleep in the loop, you would never calculate the additional real time incurred by the usleep.

So, beware that both methods, while equally useful, answer two different questions. clock() tells you CPU time used, the gettimeofday method tells you actual time elapsed.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0