Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

clock problem for Answer 2000

Posted on 1998-11-01
1
Medium Priority
?
186 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 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

609 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