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;
}