Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-10-2005, 12:31 PM   #1 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default time of execution of program, in C


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

Quote:
Originally Posted by code
#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[i][j] += a[i][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[i][j]);
}
printf("\n");
}

getch();
}
/legolas
__________________
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
legolas is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 01-10-2005, 01:04 PM   #2 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default

Read http://www.acm.uiuc.edu/webmonkeys/b...uide/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
sakumar79 is offline  
Old 01-10-2005, 06:29 PM   #3 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default

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
__________________
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
legolas is offline  
Old 01-10-2005, 08:15 PM   #4 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default

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 ). 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
http://www.cygwin.com/ - but it will require a large download.

Arun
sakumar79 is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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


 
Latest Threads
- by Sujeet
- by clinton
- by Who
- by bhaskar
- by soumya

Advertisement




All times are GMT +5.5. The time now is 11:38 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2