calculate the execution time of a program

Reply

Join Date: Oct 2004
Posts: 31
Reputation: dalaharp is an unknown quantity at this point 
Solved Threads: 0
dalaharp dalaharp is offline Offline
Light Poster

calculate the execution time of a program

 
0
  #1
Feb 25th, 2005
Hi,

i would like to calculate the execution time of my program.
is there any function available to enable it??
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: calculate the execution time of a program

 
0
  #2
Feb 25th, 2005
Store system datetime in variable at start of your procedure.

Display something like datediff(Variable,now()) at the end of your procedure.

I don't know what language your are using so I'm being vague.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: calculate the execution time of a program

 
0
  #3
Feb 25th, 2005
>I don't know what language your are using so I'm being vague.
You could assume either C or C++ and probably hit the mark. :rolleyes:

>is there any function available to enable it??
Poor man's profiler:
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. clock_t start = clock();
  5. /* Code you want timed here */
  6. printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,019
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: calculate the execution time of a program

 
0
  #4
Feb 25th, 2005
Is this like something you are thinking about?
[php]
// time the bubblesort of an array of random integers

#include <windows.h> // GetTickCount()
#include <stdio.h> // printf(), getchar()
#include <stdlib.h> // srand(), rand()

#define NUM_ITEMS 10000

void bubbleSort(int numbers[], int array_size);

int numbers[NUM_ITEMS];

int main()
{
int k;
long te, ts;

// seed random number generator
srand(GetTickCount());

// fill array with random integers
for (k = 0; k < NUM_ITEMS; k++)
numbers[k] = rand();

// start counter
ts = GetTickCount();
// perform bubble sort on array
bubbleSort(numbers, NUM_ITEMS);
// end counter
te = GetTickCount();
// show the count difference
printf("Bubble sort of 10000 random integers took %d ms\n", te - ts);

getchar(); // wait
return 0;
}

void bubbleSort(int numbers[], int array_size)
{
int finished = 0, i, j, temp;

for (i = (array_size - 1); i >= 0; i--) {
if (finished)
break;
finished = 1;
for (j = 1; j <= i; j++) {
if (numbers[j-1] > numbers[j]) {
finished = 0;
// swap
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}
[/php]
Note that a tick may not be exactly a millisecond.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 31
Reputation: dalaharp is an unknown quantity at this point 
Solved Threads: 0
dalaharp dalaharp is offline Offline
Light Poster

Re: calculate the execution time of a program

 
0
  #5
Feb 25th, 2005
i am using C.

i will check them out, thanks..
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 31
Reputation: dalaharp is an unknown quantity at this point 
Solved Threads: 0
dalaharp dalaharp is offline Offline
Light Poster

Re: calculate the execution time of a program

 
0
  #6
Feb 25th, 2005
Originally Posted by Narue
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. clock_t start = clock();
  5. /* Code you want timed here */
  6. printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
the code is working fine, except that the time it calculates is a bit lesser than what my stopclock shows.

Originally Posted by vegaseat
Note that a tick may not be exactly a millisecond.
is it the case here??
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: calculate the execution time of a program

 
0
  #7
Feb 25th, 2005
Yes. Clock ticks don't always correspond to any normal measurement of time, so any precise measurement less than a second should be done with a profiler program such as gprof. That's certainly easier than writing your own timing code. Anything that requires accuracy will be rather complicated.

The code I gave you is a good, relatively portable way to compare similar bits of code. For example, if you wanted to test the performance of two functions and compare the results, wallclock time doesn't matter because both tests will use the same time granularity. If you want the exact number of milliseconds (for example) that a piece of code takes, you're SOL with any of the simple methods.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 31
Reputation: dalaharp is an unknown quantity at this point 
Solved Threads: 0
dalaharp dalaharp is offline Offline
Light Poster

Re: calculate the execution time of a program

 
0
  #8
Feb 28th, 2005
i was just wondering, is there a convertion ratio between the clock ticks and the actual time in milliseconds?
i also understand the clock ticks vary according to the processor, right??
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: calculate the execution time of a program

 
0
  #9
Feb 28th, 2005
>is there a convertion ratio between the clock ticks and the actual time in milliseconds?
You can calculate that from the clock speed of your processor if you want. But if accurate wall clock timing is important, you should be using a real profiler to begin with instead of trying to hack a timer into your code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 3
Reputation: eivnay is an unknown quantity at this point 
Solved Threads: 0
eivnay's Avatar
eivnay eivnay is offline Offline
Newbie Poster

Re: calculate the execution time of a program

 
0
  #10
Feb 28th, 2005
Hey people I have a doubt.I learnt that there cant be a general algorithm to find out whether a given program gets exceuted in finite time or not.Is that so?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC