Answered by:
Timing homework
Question

Last week our class was given the folllowing instructions to do a program:
o Input range of N values
o Create range_time_vector
o Input number_of_cases
o for each input in the range of inputs
· Set time_sum to zero
· Set time_average to zero
· For each case
§ Start the time
§ Execute the loop
§ End the time
§ Add the time difference to time_sum
o Calculate time_average = time_sum/ number_of_cases
o Store time_average in range_time_vector at input index
So far I've been able to do this much:
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
int N =0;
int Range=0;
int NumberOfCases=0;
cout << "Enter N" <<endl;
cin >> N;
cout << "Enter Range" <<endl;
cin >> Range;
cout << "Number of cases" <<endl;
cin >> NumberOfCases;
vector<double> range_time_Vector(Range);
int K = N;
for(int i=0; i < Range; ++i)
{
double time_sum = 0.0;
double time_average = 0.0;
for (int m=1 ; m <= NumberOfCases; ++m)
{
cout << "for m=" << m << " K=" << K << endl;
}
}
return 0;
}
Can Someone help me with the following steps:
For each case§ Start the time
§ Execute the loop
§ End the time
§ Add the time difference to time_sum
o Calculate time_average = time_sum/ number_of_cases
o Store time_average in range_time_vector at input index
I tried googling and going to cplusplus.com, but wasn't able to find anything to help me with my program. Changed type noyfb1 Wednesday, March 10, 2010 5:35 PM
Answers

Well, to calculate time, you have a couple of options. std::clock() can be used to get "ticks" that have elapsed since your program began. The resolution may not be what you need. You get CLOCKS_PER_SEC "ticks" every second (this is 1000 in VS2008).
If you want to use Microsoft specific code, try QueryPerformamceCounter and QueryPerformanceFrequency for highprecision timing. Marked as answer by Nancy Shao Tuesday, March 16, 2010 5:33 AM
All replies

> Can Someone help me with the following steps:
These forums aren't really for homework assignments, however here's some
hints that may get you a bit futher ;)
> § Start the time
I'd replace the above with "get the current time" (and save it)
> § End the time
Get the time now.
Dave 
Well, to calculate time, you have a couple of options. std::clock() can be used to get "ticks" that have elapsed since your program began. The resolution may not be what you need. You get CLOCKS_PER_SEC "ticks" every second (this is 1000 in VS2008).
If you want to use Microsoft specific code, try QueryPerformamceCounter and QueryPerformanceFrequency for highprecision timing. Marked as answer by Nancy Shao Tuesday, March 16, 2010 5:33 AM