Solved

clock problem for Answer 2000

Posted on 1998-11-01
1
180 Views
Last Modified: 2010-04-02
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
Comment
Question by:demami
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1176768
1. read class stopwatch

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question