Hello again people, I've got some questions about timing that I hope can be answered by someone.
I'm trying to use the functions getrusage or times, or the time command to time my programs. Now I've got this piece of code that uses getrusage, and my first question is a bit of a daft one, can anyone please tell me what unit t is given in? I got 1086556160 last time I ran the program. Is this milllionths of a second or what?

#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<sys/resource.h>

double getcputime();

int main(void)
{
int i, n, a;
double t;

n = 13;
printf("The 5-times Table\n");
for(i=1;i<n;i++)
{
	a = (i * 7);
	printf("%d\t%d\n", i, a);
}

printf("\n");
t = getcputime();
printf("The total time taken by the system is: %d (in decimal format)\n", t);

return 0;
}

double getcputime(void)
{
	double t;
	       struct timeval tim;        
               struct rusage ru;
	       getrusage(RUSAGE_SELF, &ru);
               tim=ru.ru_stime;
               t=(double)tim.tv_sec * 1000000.0 + (double)tim.tv_usec;        
               return t;
}

My second question is more general (for now), does anyone know why very few textbooks talk about timing programs, and even if you wanted to use the system clock, surely that is only accurate to seconds, and therefore not what is needed to time a relatively fast program?

1. Don't know the first question.

2. Functions must often be run hundreds or even thousands of times in order to get measureable time. The clock() function is in milliseconds and usually sufficient. So, to get a measurable amount of time

start timer
run the function 1,000 times
stop timer
delta time (stop - start) / 1000 is the average time per iteration of the function.

nonetheless, i found out that clock() works different depending on which compiler you are working... 4 example... in dev c++, i measured clock measured 1/1000... but turbo c++ measured 1/20...

the thing is... i don't know why...

If you need to using some timing functions to time your programs output then theres nothing more perfect than using windows sleep command.

here are some header files to use for time that you may want to look into , or include into your programs that need timeing before an even occurs.


#include <windows.h>
#include <dos.h>

there are timing functions in them all . Personally for particular needs my favorite sleep(5000);
which makes the applicatoin sleep in miliseconds. in this case would be 5 seconds.

#include <TIME.h> also has some member functions you may be interested in looking up.

you may use the example in someting like so.


#include <iostream>
#include <dos.h>
#include <windows.h>
#include <stdlib.h>

using namespace std;

int main(char argc,char *argv[])
{
cout<<"hello there hows it going"<< endl;
Sleep(5000);
cout<<"H"<< endl;
Sleep(4000);
cout<<"E"<< endl;
Sleep(3000);
cout<<"Y"<< endl;
Sleep(2000);
cout<<" Is for Horses" << endl;
system("pause");
return 0;
};

If you need to using some timing functions to time your programs output then theres nothing more perfect than using windows sleep command.

The problem is, this command doesn't work on every program... i.e. Turbo C++ doesn't recognize this command...

Get DEV ! Why are you stuck with Turbo C++ ? BloodShed is problably one of the best compilers out there and compiles efficiantly.

Im not a borland person, but bloodshed makes a nice compiler :) Nice windows API help as well easly supported and often found for bloodshed users.

Get DEV ! Why are you stuck with Turbo C++ ? BloodShed is problably one of the best compilers out there and compiles efficiantly.

BloodShed makes a super nice compiler with alot of library support.
Ide definitely go with nothing else, other than my ripped version of MSVSC++ :)
Its more favorite to me over MSVC++ on some terms.

Get DEV ! Why are you stuck with Turbo C++ ? BloodShed is problably one of the best compilers out there and compiles efficiantly.

it's not 4 me that i'm talking... as i've explained in others posts, i rather work in bloodshed's than in borland's, but, there's lots of peeps that must work in turbo because of work, college, university (like me), or other circumstances... so, i recommend you post solutions for users of any kind of compiler, because we all not have the privilege to work in our predilect compiler of c++...

nonetheless, i found out that clock() works different depending on which compiler you are working... 4 example... in dev c++, i measured clock measured 1/1000... but turbo c++ measured 1/20...

the thing is... i don't know why...

clock_t start, end;
double cpu_time_used;
start = clock();   
/* whatever */
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

First I assume your question is how to find out the performance/time of your function. In this case I really donno how you can use Sleep ?!
As I use timing quite frequently I have the following code (similar to Vijyan's) I use to make it easy to use.

#include <ctime>
#include <iostream>

using namespace std ;

#define CREATE_CLOCK() clock_t ____start, ____diff = 0 ;
#define START_CLOCK() ____start = clock() ;
#define REPORT_CLOCK_DIFF() ____diff = clock() - ____start ; \
std::cout << std::endl << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << std::endl \
<< "difference = " << ____diff << ", diff/CLOCKS_PER_SEC = " << ____diff/CLOCKS_PER_SEC << std::endl ;

int main()
{
        CREATE_CLOCK()

        START_CLOCK()
        /*do your stuff*/
        REPORT_CLOCK_DIFF()

        START_CLOCK()
        /*do your stuff*/
        REPORT_CLOCK_DIFF()

        return 0 ;
}

Output is like this:
---------------------------------------------------
On Solaris 10 with Sun WorkShop 6 update 2 C++
CLOCKS_PER_SEC = 1000000
difference = 0, diff/CLOCKS_PER_SEC = 0

CLOCKS_PER_SEC = 1000000
difference = 0, diff/CLOCKS_PER_SEC = 0
---------------------------------------------------
On Windows 2000 with VS 6.0:
CLOCKS_PER_SEC = 1000
difference = 0, diff/CLOCKS_PER_SEC = 0

CLOCKS_PER_SEC = 1000
difference = 0, diff/CLOCKS_PER_SEC = 0
Press any key to continue
---------------------------------------------------
As you can see CLOCKS_PER_SEC is different for different compilers (or may be it's due to hardware, not sure).
diff/CLOCKS_PER_SEC gives you the time taken for /*do your stuff*/ in seconds.

>> My second question is more general (for now), does anyone know why very few textbooks talk about timing programs
My guess is coz usually time related APIs are pretty specific to compilers (clock() is the only thing I've found working on both compilers I use).

nonetheless, i found out that clock() works different depending on which compiler you are working... 4 example... in dev c++, i measured clock measured 1/1000... but turbo c++ measured 1/20...

the thing is... i don't know why...

In the original MS-DOS 6.X and older operating systems the clock speed default was 18 ticks per second, thus clock() return 1/18. Programs that wanted to use milliseconds had to deliberately reprogram the clock speed to 1000 ticks per second and reset it back to 18 before the program exited. If the program failed to reset the clock speed then all sorts of funny, and sometimes disasterous, things would happen.

Thanks everyone for all your posts. Will try different solutions and see what works. Thing is, does the fact that noone has mentioned getrusage() mean that noone here uses it?

Thanks everyone for all your posts. Will try different solutions and see what works. Thing is, does the fact that noone has mentioned getrusage() mean that noone here uses it?

never heard of it until yesterday. Its a linux/unix-specific function and most posters here use MS-Windows os.

clock_t start, end;
double cpu_time_used;
start = clock();   
/* whatever */
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

I've added an adaptation of this into my program. I print out cpu_time_used in floating point format. Is this value in seconds, because I have divided by CLOCKS_PER_SEC?

never heard of it until yesterday. Its a linux/unix-specific function and most posters here use MS-Windows os.

Ah right, interesting. Well I use MS-Windows too, but I have to run my C programs through the compilers on the Unix servers at my universities. Which I guess means I'm sort of working on unix doesn't it?

>>Which I guess means I'm sort of working on unix doesn't it?

Yes and No. You are working on both environments and will probably have to program for both by using preprocessor directives for each compiler

#if defined _WINDOWS
// do MS-Wndows specific code here
#else
// do *nix specific code here
#endif

I know the above looks like a pain, but that's the way portable programs are written when os-specific api calls and even compiler-specific calls are made.

In your case, I think I would install cygwin on your ms-windows computer and use gnu compiler so that you don't have to do that sort of messy preprocessor stuff as shown above.

I've added an adaptation of this into my program. I print out cpu_time_used in floating point format. Is this value in seconds, because I have divided by CLOCKS_PER_SEC?

yes, it is in seconds. and it is completely portable. (ANSI/POSIX)
you would need to write platform specific code if and only if you find that the resolution of CLOCKS_PER_SEC is too low for your purposes on a particular system.

Thanks everyone for all your posts. Will try different solutions and see what works. Thing is, does the fact that noone has mentioned getrusage() mean that noone here uses it?

In short no, AFAIK ppl don't use getrusage() for this purpose. Because you can as well use time/timex for this purpose.
Difference between what others have suggested here and getrusage() is that getrusage() gives you something very similar to what time (or timex) commands (on unix) will give you, viz overall CPU time taken. Whereas other techniques will give you time for any smaller part of your program. More like white-box and black-box.

will probably have to program for both by using preprocessor directives for each compiler

#if defined _WINDOWS
// do MS-Wndows specific code here
#else
// do *nix specific code here
#endif

Given that the code posted by me/Vijayan works both on windows and unix #ifdefs won't be needed in this particular case.

Comments
good point

Given that the code posted by me/Vijayan works both on windows and unix #ifdefs won't be needed in this particular case.

agreed that kind of messy stuff isn't needed when using portable functions like clock(). I think I was referring to using non-portable code such as getrusage(). And there are, of course, hundreds of other non-portable examples.

Thanks very much people, I am now timing my programs using clock(). Seems to give fairly decent graphs, although I think I need to run the programs more often to get a better average.

Still, all input has been very much appreciated.

agreed that kind of messy stuff isn't needed when using portable functions like clock(). I think I was referring to using non-portable code such as getrusage(). And there are, of course, hundreds of other non-portable examples.

Yup, for non-portable things it would be needed. That's why I mentioned "this particular case". :)

to find the timing in c using the clock() function are we supposed to initialise any special header......... to be more direct what header is needed for that clock() function.........

This question has already been answered. Start a new discussion instead.