Solved

# clock problem for Answer 2000

Posted on 1998-11-01
174 Views
Here is my problem. How can I modify this code to time the "enter" operation? I'm using the Stopwatch class unmodified to do the timing. I'm looking for the program to print the total time, the average time per "enter" and the min and max time for the "enter"operations.

while (cin>>name)
{

table.enter(name);

}
--------------------------
class Stopwatch
{public:
Stopwatch() : sum (0) {}
void start() {begin = clock();}
double stop() {double res = (double) (clock()-begin);
sum += res;
return res / CLOCKS_PER_SEC;
}
double total() {return sum / CLOCKS_PER_SEC;}
private:
double sum;
clock_t begin;
};
0
Question by:demami
1 Comment

LVL 8

Accepted Solution

ID: 1176768

the constructor sets the sum variable to 0
the start function gets the start function gets the current tick count
the stop function subtracts the current tick count and subtracts the starting time
the total function gives you the ticks / ticks per second : i.e. total seconds

i think the idea of stopwatch is you can start and stop it multiple times to time a sinel operation.

2. okay to time it you should
(a) construct a stop watch (declare a variable of type stop watch)
(b)  call start to start the timer
(c) do the operation
(d) call stop to stop the operation
(e) call total to get the time for the operation

3. Tip #2 times one operation, so calculate the values you want
(a) the current time you got from 2(e)
(b) count the number of times you call enter (keep a running total each time you do it, by adding an extra variable to your code, and incrementing it each time you call the enter function.  divide sum of all times by number of times done to get the average
(c) keep the running minimum and maximum.  The first time you do the enter operaton this is by definition both the minimum and maximum value.  When you loop 2nd, 3rd, 4th, etc. time compare the previous min/max against the latest time - if less/more then this is new min/max.  You need two new variables for this

I won't give you the code straight out as this sounds like a school problem.  Have a go, using what I told you above, if you have specific problem, post the code as you get on, and ask further questions.
0

## Featured Post

### Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticallâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

#### Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!