Solved

clock problem for Answer 2000

Posted on 1998-11-01
1
178 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

821 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