| | |
Problem in measuring program execution time with c
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 3
Reputation:
Solved Threads: 0
hello everyone.
i m monjuri. i want to find out the execution time of a C program. it works fine. but i want the time in milisecond or microsecond. Can anybody help me???
here is my code:
/*insertion code*/
#include <stdio.h>
#include <time.h>
#define maxsize 10
int A[maxsize];
int value,j,i;
insertionSort(int A[],int s){
int i, j, t;
for (i=1; i<s; i++)
{
j=i;
t=A[j];
while (j>0 && A[j-1]>t)
{
A[j]=A[j-1];
j--;
}
A[j]=t;
}
}
void printarr(int a)
{
int i;
for(i=0;i<a;i++)
{
printf("%d",A[i]);
printf("\n");
}
}
//Global Variables
double timer;
double timer2;
void startTimer()
{
timer = clock();
}
double stopTimer()
{
timer2 = clock();
timer = timer2 - timer;
return timer;
}
main()
{
int i,s;
double time;
printf("enter the number of numbers to be entered \n");
scanf("%d",&s);
for(i=0;i<s;i++)
{
printf("enter the number \n" );
scanf("%d",&A[i]);
}
printf("array before sorting ");
printarr(s);
insertionSort(A,s);
printf("array after sorting");
printarr(s);
startTimer();
time= stopTimer();
printf(" the execution time %lf",time);
}
Thank u.
i m monjuri. i want to find out the execution time of a C program. it works fine. but i want the time in milisecond or microsecond. Can anybody help me???
here is my code:
/*insertion code*/
#include <stdio.h>
#include <time.h>
#define maxsize 10
int A[maxsize];
int value,j,i;
insertionSort(int A[],int s){
int i, j, t;
for (i=1; i<s; i++)
{
j=i;
t=A[j];
while (j>0 && A[j-1]>t)
{
A[j]=A[j-1];
j--;
}
A[j]=t;
}
}
void printarr(int a)
{
int i;
for(i=0;i<a;i++)
{
printf("%d",A[i]);
printf("\n");
}
}
//Global Variables
double timer;
double timer2;
void startTimer()
{
timer = clock();
}
double stopTimer()
{
timer2 = clock();
timer = timer2 - timer;
return timer;
}
main()
{
int i,s;
double time;
printf("enter the number of numbers to be entered \n");
scanf("%d",&s);
for(i=0;i<s;i++)
{
printf("enter the number \n" );
scanf("%d",&A[i]);
}
printf("array before sorting ");
printarr(s);
insertionSort(A,s);
printf("array after sorting");
printarr(s);
startTimer();
time= stopTimer();
printf(" the execution time %lf",time);
}
Thank u.
Remember seeing this?
What about this?
Read This Before Posting
•
•
•
•
Our Software Development forum category encompasses topics related to application programming and software design. When posting programming code, encase it in [code], [code=syntax], where 'syntax' is any language found within our Code Snippets section, or [icode], for inline code, bbcode tags. Also, to keep DaniWeb a student-friendly place to learn, don't expect quick solutions to your homework. We'll help you get started and exchange algorithm ideas, but only if you show that you're willing to put in effort as well.
Read This Before Posting
•
•
•
•
c Syntax (Toggle Plain Text)
/*insertion code*/ #include <stdio.h> #include <time.h> #define maxsize 10 int A[maxsize]; int value, j, i; insertionSort ( int A[], int s ) { int i, j, t; for ( i = 1; i < s; i++ ) { j = i; t = A[j]; while ( j > 0 && A[j-1] > t ) { A[j] = A[j-1]; j--; } A[j] = t; } } void printarr ( int a ) { int i; for ( i = 0; i < a; i++ ) { printf ( "%d", A[i] ); printf ( "\n" ); } } //Global Variables double timer; double timer2; void startTimer() { timer = clock(); } double stopTimer() { timer2 = clock(); timer = timer2 - timer; return timer; } int main() { int i, s; double time; printf ( "enter the number of numbers to be entered \n" ); scanf ( "%d", &s ); for ( i = 0; i < s; i++ ) { printf ( "enter the number \n" ); scanf ( "%d", &A[i] ); } printf ( "array before sorting " ); printarr ( s ); insertionSort ( A, s ); printf ( "array after sorting" ); printarr ( s ); startTimer(); time = stopTimer(); printf ( " the execution time %lf", time ); getchar(); getchar(); }
Last edited by iamthwee; Jul 29th, 2008 at 6:15 pm.
*Voted best profile in the world*
Regrettably, in practice no way to measure time intervals with better than ~15 MILLIsecond precision in platform-independent manner (i.e with C RTL functions including clock()). The CLOCKS_PER_SECOND macros per se does not guarantee that your program may catch every tick with this frequency.
I don't know what's your target platform. For Windows I have my own CPU performance counter based C++ code to measure time intervals with ~1.5 MICROseconds precision (on Windows XP Pro installation). It works perfectly and it's so easy to adopt it in C... but come back to the 1st statement of this paragraph...
I don't know what's your target platform. For Windows I have my own CPU performance counter based C++ code to measure time intervals with ~1.5 MICROseconds precision (on Windows XP Pro installation). It works perfectly and it's so easy to adopt it in C... but come back to the 1st statement of this paragraph...
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: File parsing going awry
- Next Thread: File Format Conversion/Compression
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer pointers posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






