I'm using clock() to measure the speed of my code (testing the speed of passing by value vs passing by reference in function calls). The problem is, the speed returned by my code is always 0.0000000
Can you check if I'm using the clock() function correctly?
the function is finding the number in the array that is closest to a number given by the user. Here is the code for the pass by value function.
Thanks for the help!
relevant code:
Can you check if I'm using the clock() function correctly?
the function is finding the number in the array that is closest to a number given by the user. Here is the code for the pass by value function.
Thanks for the help!
relevant code:
Code:
double Array::closestNumber (double num) {
double closest = 0;
int decimalPlaces = 15;
clock_t start;
clock_t end;
double speed;
start = clock();
for (int i = 0; i < this->length; i++) {
if (fabs(this->contents[i] - num) < fabs(closest - num)){
closest = this->contents[i];
}
}
end = clock();
speed = ((double)end-(double)start)/CLOCKS_PER_SEC;
cout << "The time elapsed is: " << endl;
cout << fixed << setprecision(decimalPlaces) << speed <<endl;
return closest;
}
Comment