PDA

View Full Version : How to use the C time function to compile in Windows instead of Unix?


pandol
07-13-2006, 03:17 AM
Hello everybody,

The program which I attach estimates the time taken to populate a matrix with its elements. Though this program does run if I compile it under Unix, it does not run if I compile it in an ordinary compiller. My question is: When we use the time command in a C program do we always have to compile the program under Unix only and not ordinary compilers? Is it possible to make the program compile in ordinary compilers? Which is the correct program, without errors for a windows compiler?


#include <stdio.h>
#include <sys/time.h> /* Necessary libraries for the time complexity */
#include <time.h>
#include <sys/resource.h>

double gettime(void);

int main (void)
{
int i,j;
int arr[200][200];

double time;

time = gettime();

for (i=0; i<199; i++)
{
for (j=0; j<199; j++)
{
arr[i][j]=10*i+j;
printf("%d\n", arr[i][j]);
}
}


printf("Time is %f\n", (gettime()-time)/100000.0);
system("PAUSE");
return 0;
}


double gettime(void)
{
struct rusage r;
double tt;
getrusage(0, &r);
tt=1000.0*(double)(r.ru_utime.tv_sec)+(double)(r.ru_utime.tv_usec)/1000.0+
1000.0*(double)(r.ru_stime.tv_sec)+(double)(r.ru_stime.tv_usec)/1000.0;
return tt;
}

Regards

oracleguy
07-13-2006, 06:14 PM
This is guess in the dark here but if sys/time.h doesn't exist try including ctime instead.