Timing a program (process) from another program 
Author Message
 Timing a program (process) from another program

Hi!
I need to write a program that start a second program and also time
the execution time for the second program. I did a try (following
code) but of course it did not work properly. I’m very glad if
someone could help me.
/jola

/************************************************/
void process(void){
pid_t pid;
if((pid = fork())<0)
    printf("error at 1\n");
  else if (pid == 0){
    if(execl("/home/xsajlar/Test_Prog/timing/run",
"run",NULL) <0)
     printf("error at 2\n");

   }
   if(waitpid(pid, NULL,0)<0)
     printf("error at 3\n");

Quote:
}

main(){

  clock_t start, stop, duration;
  struct timeval first_time, sec_time;
  float tot_time;

  gettimeofday(&first_time, NULL);
  start = clock();
  /**Want to time this part**/  
  process();
 /**************************/  

  stop = clock();
  gettimeofday(&sec_time, NULL);
  tot_time = (float)(sec_time.tv_usec - first_time.tv_usec)*1.0e-6;

  printf("first_time = %f\n",(float)first_time.tv_usec);
  printf("sec_time = %f\n",(float)sec_time.tv_usec);
  printf("tot from gettimeofday = %f sec (real time)\n",tot_time);

  duration = stop - start;
  printf("tot time fom clock =  %f sec (user time) \n",
     ((double) (stop - start)) / CLOCKS_PER_SEC);
  exit(0);
 return 0;

Quote:
}

/************************************************/


Mon, 21 Nov 2005 17:03:44 GMT  
 Timing a program (process) from another program


Quote:
> Hi!
> I need to write a program that start a second program and also time
> the execution time for the second program. I did a try (following
> code) but of course it did not work properly. I&#8217;m very glad if
> someone could help me.
> /jola

i believe this is how it works:

clock_t    startTime = clock();
/* timed code here */
clock_t    stopTime = clock();
clock_t    totalTime = stopTime - startTime;
clock_t    totalTimeInSeconds = totalTime / CLOCKS_PER_SEC;

and if you need a more precise value, you could use floats (i'm assuming
that clock_t can be cast to a float, if not someone correct me)
float        totalTime = (float)stopTime - (float)startTime;
float        totalTimeInSeconds = totalTime / (float)CLOCKS_PER_SEC;

not sure if that last cast is necessary.  if CLOCKS_PER_SEC is #define'd, it
should work fine without it

chris



Mon, 21 Nov 2005 20:40:31 GMT  
 Timing a program (process) from another program


Quote:

> float        totalTime = (float)stopTime - (float)startTime;
> float        totalTimeInSeconds = totalTime / (float)CLOCKS_PER_SEC;

> not sure if that last cast is necessary.  if CLOCKS_PER_SEC is #define'd,
it
> should work fine without it

just realize that the casts are unnecessary in the calculation for totalTime
there...i just have a habit of casting first, asking questions later...i
need to break that habit probably :)

chris



Mon, 21 Nov 2005 20:44:23 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. moderating program's usage of processing time

2. C Program->Process->Same C Program

3. Announcing a program/process robustness testing program.

4. monitoring the process time of a child process

5. How to convert local time to gmt using a local variable time zone per process/thread

6. how to minimize program started with Process.Start

7. How can I get more power on my program in Kill method at Process class

8. help me with an email processing program - filtering

9. C program string processing - Extreme Urgent !!!!!!

10. !Help - Listing and killing processes and programs in Windows

11. +Socket Programming - - Automating login process

12. word-processing program

 

 
Powered by phpBB® Forum Software