time of execution of program, in C

Status
Not open for further replies.

legolas

Padawan
hi,

suppose i hav a matrix multiplication program written in C... how do i find the time it takes to calculate the result. how do i do that? also, i need the syntax for both windows and linux... thk u..

code if necessary

code said:
#include <stdio.h>
#include <stdlib.h>


void main()
{
int i,j,k;
int a[3][3] = {1,2,3,4,5,6,7,8,9};
int b[3][3] = {9,8,7,6,5,4,3,2,1};
int c[3][3] = {0};
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
c[j] += a[k] * b[k][j];
}
}
}

printf("\n");
printf("the output is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[j]);
}
printf("\n");
}

getch();
}


/legolas
 

sakumar79

Technomancer
Read *www.acm.uiuc.edu/webmonkeys/book/c_guide/2.15.html for details of time.h header file...

Excerpt:
2.15.3 clock

Declaration:
clock_t clock(void);
Returns the processor clock time used since the beginning of an implementation-defined era (normally the beginning of the program). The returned value divided by CLOCKS_PER_SEC results in the number of seconds. If the value is unavailable, then -1 is returned

Example:

#include<time.h>
#include<stdio.h>

int main(void)
{
clock_t ticks1, ticks2;

ticks1=clock();
ticks2=ticks1;
while((ticks2/CLOCKS_PER_SEC-ticks1/CLOCKS_PER_SEC)<1)
ticks2=clock();

printf("Took %ld ticks to wait one second.\n",ticks2-ticks1);
printf("This value should be the same as CLOCKS_PER_SEC which is %ld.\n",CLOCKS_PER_SEC);
return 0;
}

From the above example, it is easy to see how to find time of execution in seconds....
{
...
int timeTaken;
clock_t ticks1, ticks2;

ticks1=clock();
{things to be done for which time taken is to be calculated}
ticks2=clock();

timeTaken=ticks2/CLOCKS_PER_SEC-ticks1/CLOCKS_PER_SEC;
}

Hope this helps...
Arun
 
OP
L

legolas

Padawan
gr8! it worked. but the syntax for linux C, is it the same too?? i hav seen the o/p in windows and works absolutely fine!

/legolas
 

sakumar79

Technomancer
should be... just check that u have the time.h file. If ur compiler can compile, u should be able to run it (assuming ur logic is right :wink: ). If u have a windows system and want to check how it will work in Linux, install Cygwin - a Linux emulator for Windows. It is available at
*www.cygwin.com/ - but it will require a large download.

Arun
 
Status
Not open for further replies.
Top Bottom