Thread: <time.h> in program

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    34

    <time.h> in program

    I am trying to find out how much time it takes to complete an execution of a program in Linux, by executing it from a new process. I am using the <time.h> time() function, but I get strange numbers pushed into the clockTimeA vector, but can't figure out why. Any suggestions as to what the problem may be? Thanks.

    Here is my code:
    Code:
    #include <ctime>
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <cmath>
    #include <sys/types.h>
    #include <unistd.h>
    
    using namespace std;
    
    int main()
    {
     pid_t pid;
    
     time_t start, finish; //time variables
    
     vector <double> clockTimeA;
     double sdA;
     double meanA = 0;
     double varianceA = 0;
    
     ofstream OUTPUTFILE;
     OUTPUTFILE.open("Runtime_Data_Fork_Linux_A.txt");
    
     pid = fork();
    
    for(int i = 0; i <= 1; i++)
    {
    
    if(pid == 0)
    
    { 
     start = time(NULL);
    
     //Process A
    
      ///////////////////////////////////////////////  
    
      execlp("/A", "A", NULL);
    }// end if loop
    
    if (pid > 0)
     {
      wait();
      cout <<"waiting for child process to finish" << "\n"; 
      //get finish time for process A
    
      finish = time(NULL);
    
     cout << "A started at: " << " " << start << " " << "and finished at: " << finish << " " << "\n";
    
     clockTimeA.push_back((double) (finish-start) ) ;
     cout << "1st time is: " << clockTimeA[0] << "\n" ;
     }
    
    } // end 30 times loop
      
    
    if(pid > 0)
    {
      for(int i = 0; i < clockTimeA.size(); i++)
      {
       OUTPUTFILE << ((double) (finish-start)) << "\n";
       meanA += clockTimeA[i]/2; 
      }
    
      OUTPUTFILE << "\n" << "Mean" << "\n" << meanA << "\n" << "\n";
    
     
      for(int i = 0; i < clockTimeA.size(); i++)
      {
       varianceA += (pow((clockTimeA[i] - meanA), 2))/2 ;
      }
    
      sdA = sqrt(varianceA);
      OUTPUTFILE << "StdDev" << "\n" << sdA <<  endl;
    
    }
     return 0;
    
    }
    Last edited by Bnchs400; 10-23-2007 at 05:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM