Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use the C time function to compile in Windows instead of Unix? 1

Status
Not open for further replies.

pandol

Programmer
Jul 12, 2006
1
GB
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[j]=10*i+j;
printf("%d\n", arr[j]);
}
}


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


double gettime(void) /* Function defiition, Code taken under agreement with Professor J.P. Fitch */
{
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
 
Well first, there's no such thing as an "ordinary compiler". :)

Next, the getrusage() function looks like a UNIX specific function, not an ANSI C function; so it doesn't exist on Windows.

Use the clock() function instead. Then it will compile on any ANSI C compiler.
 
The include file sys/resource.h is non standard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top