Solved

clock problem for Answer 2000

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
getting a fire fox page (view page source ) in c++ 6 42
Problem with SqlConnection 4 168
trigs fail! I thought I knew how to do trignometry 3 65
Precision Problem in C++ 7 31
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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 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.

813 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now