CPSC 427a: Object-Oriented Programming

Michael J. Fischer

(with thanks to Ewa Syta for help with the slides)

Lecture 14
October 20, 2011

subsection in toc

More on Course Goals

Low-level details

Example picky detail

Efficient use of resources

Efficiency is concerned with making good use of available resources:

Strategy for improving efficiency: Reuse and recycle. Maintain a pool of currently unused objects and reuse rather than recreate when possible.

In the case of memory blocks, this pool is often called a free list.

Efficiency measurement

A first step to improving efficiency is to know how the resources are being used.

Measuring resource usage is not always easy.

The next demo is concerned with measuring execution time.

Demo: Stopwatch

How to measure run time of a program

High resolution clocks

Measuring time in real systems

Realtime measurements

StopWatch is a class I wrote for measuring realtime performance of code.

It emulates a stopwatch with 3 buttons: reset, start, and stop.

At any time, the watch displays the cumulative time that the stopwatch has been running.

(See demo.)

HirezTime class

HirezTime is a wrapper class for the system-specific functions to read the clock.

It hides the details of the underlying time representation and provides a simple interface for reading, computing, and printing times and time intervals.

HirezTime objects are intended to be copied rather than pointed at, and to behave like other numeric types.

Versions of HirezTime

There are two versions: 12-StopWatch (Linux/Unix/Darwin) Function gettimeofday() returns the clock in a struct timeval, which consists of two long ints representing seconds and microseconds. The resolution of the clock is system-dependent, typically 1 millisecond. 12-StopWatch-hirez (Linux only) Function clock_gettime() returns the clock in a struct timespec, which consists of two long ints representing seconds and nanoseconds. The resolution of the clock is system-dependent and can be obtained with the clock_getres() function.

HirezTime structure

Printing a HirezTime number

Something seemingly simple like printing HirezTime values is not so simple. Naively, one might write:

    cout << t.tv_sec << "." << t.tv_usec;

where tv_sec and tv_usec are the seconds and microseconds fields of a timeval structure.

If t represents 2 seconds and 27 microseconds, then what would print is 2.27, not the correct 2.000027.

The class contains a print function that fixes this problem.

StopWatch class

StopWatch contains five member variables to record

All functions are inline to minimize inaccuracies of measurement due to the overhead withing the stopwatch code itself.

Casting a StopWatch to a HirezTime

An operator extension defines a cast for reading the cumulative time from a stop watch:

   operator HirezTime() const { return cumSpan; }

Thus, if sw is a StopWatch instance,

   cout << sw;

will print sw.cumSpan using sw.print().

Why it works

This works because operator<<() is not defined for righthand operands of type StopWatch but it is defined for HirezTime.

The compiler then coerces sw to something that is acceptable to the << operator.

Because operator HirezTime() is defined for class StopWatch, the compiler will invoke it to obtain a HirezTime object, for which << is defined.

Note that a similar coercion happens when one writes

   if(!in) {}

to test if an istream object in is open for reading. Here, the istream object is coerced to a bool because operator bool() is defined inside the streams package.

Demo: Hangman Game

Game Rules

Hangman game Well-known letter-guessing game.

Start with a hidden puzzle word.

Player guesses a letter.

Player wins when puzzle word is uncovered.

Player loses after 7 bad guesses