Unformatted text preview:

OutlineMore on Course GoalsDemo: StopwatchDemo: Hangman GameGame RulesOutline Goals Stopwatch HangmanCPSC 427a: Object-Oriented ProgrammingMichael J. Fischer(with thanks to Ewa Syta for help with the slides)Lecture 14October 20, 2011CPSC 427a, Lecture 14 1/22Outline Goals Stopwatch HangmanMore on Course GoalsDemo: StopwatchDemo: Hangman GameGame RulesCPSC 427a, Lecture 14 2/22Outline Goals Stopwatch HangmanMore on Course GoalsCPSC 427a, Lecture 14 3/22Outline Goals Stopwatch HangmanLow-level detailsIC++ is a large and complicated language with many quirksand detailed rules.IOne goal of this course is for you to learn how to dealeffectively with a complex system where it is not feasible toknow everything about it before beginning to use it.ILow-level details tend to be easy to find in the documentationonce you know what to look for.IWhat’s important to learn is the overall roadmap of thelanguage and where to look to find out more.CPSC 427a, Lecture 14 4/22Outline Goals Stopwatch HangmanExample picky detailIIf you do not supply a constructor for a class, C++automatically generates a null default constructor for you,that is, one that takes no parameters and does nothing.IIf you do define a constructor, the default constructor is notgenerated. If you want it, you then need to explicitly define it,e.g.,MyClass() {}IWhat if you didn’t know this and assumed the defaultconstructor was pre-defined? The compiler would give you anerror comment about it not being defined, and you would bestarted on the track of trying to figure out why.CPSC 427a, Lecture 14 5/22Outline Goals Stopwatch HangmanEfficient use of resourcesEfficiency is concerned with making good use of availableresources:ITime (how fast a program works)IMemory (how much memory the program requires)IOther resources that are scarce and relatively costly to create:INetwork connections (TCP sockets)IDatabase connectionsStrategy for improving efficiency: Reuse and recycle. Maintain apool of currently unused objects and reuse rather than recreatewhen possible.In the case of memory blocks, this pool is often called a free list.CPSC 427a, Lecture 14 6/22Outline Goals Stopwatch HangmanEfficiency measurementA first step to improving efficiency is to know how the resourcesare being used.Measuring resource usage is not always easy.The next demo is concerned with measuring execution time.CPSC 427a, Lecture 14 7/22Outline Goals Stopwatch HangmanDemo: StopwatchCPSC 427a, Lecture 14 8/22Outline Goals Stopwatch HangmanHow to measure run time of a programIThere is no standard procedure in C++ for accuratelymeasuring time.ITime measurement depends on the software clocks providedby your computer and operating system.IClocks advance in discrete clicks called jiffies. A jiffy on theZoo linux machines is one millisecond (0.001 seconds) long.IEven if the clock is 100% accurate, the measured time can beoff by as much as one jiffy.IHence, times shorter than tens of milliseconds cannot bedirectly measured with much accuracy using the standardsoftware clock.CPSC 427a, Lecture 14 9/22Outline Goals Stopwatch HangmanHigh resolution clocksILinux also provides high resolution clocks based on CPUtimers.IHigh resolution clocks are useful to the operating system fortask scheduling and timeouts.IThey are also available to the user for higher-precision timemeasurements.IBe aware that reading the clock involves a kernel call thattakes a certain amount of time. This itself may limit theaccuracy of timing measurements, even when the clockresolution is sufficiently high for the desired accuracy.ISee man 7 time for more information about linux clocks.CPSC 427a, Lecture 14 10/22Outline Goals Stopwatch HangmanMeasuring time in real systemsIMeasuring code efficiency in real systems is challenging. Manyfactors can influence the results that are hard to control.IOther process running on the same machine.ITime spent in the OS moving data on and off disks.IMemory caching behavior.ILacking a controlled laboratory environment, one can still takemeasures to improve accuracy of the tests:IDo some tests to determine what factors seem to have asizable effect on the run time, e.g., the first run of a programis likely to be slower than subsequent runs because of caching.IRun the same test several times to get a feeling for thevariance of results.IMake sure the optimizer isn’t optimizing away code that youthink is being executed.CPSC 427a, Lecture 14 11/22Outline Goals Stopwatch HangmanRealtime measurementsStopWatch is a class I wrote for measuring realtime performanceof code.It emulates a stopwatch with 3 buttons: reset, start, and stop.At any time, the watch displays the cumulative time that thestopwatch has been running.(See demo.)CPSC 427a, Lecture 14 12/22Outline Goals Stopwatch HangmanHirezTime classHirezTime is a wrapper class for the system-specific functions toread the clock.It hides the details of the underlying time representation andprovides a simple interface for reading, computing, and printingtimes and time intervals.HirezTime objects are intended to be copied rather than pointedat, and to behave like other numeric types.CPSC 427a, Lecture 14 13/22Outline Goals Stopwatch HangmanVersions of HirezTimeThere are two versions:12-StopWatch (Linux/Unix/Darwin) Function gettimeofday()returns the clock in a struct timeval, whichconsists of two long ints representing seconds andmicroseconds. The resolution of the clock issystem-dependent, typically 1 millisecond.12-StopWatch-hirez (Linux only) Function clock gettime()returns the clock in a struct timespec, whichconsists of two long ints representing seconds andnanoseconds. The resolution of the clock issystem-dependent and can be obtained with theclock getres() function.CPSC 427a, Lecture 14 14/22Outline Goals Stopwatch HangmanHirezTime structureIIn C++, struct T and class T are very similar. In bothcases, T becomes a new type name.Istruct members are public by default.class members are private by default.IHirezTime is derived from struct timeval or structtimespec, depending on the version.IIt uses protected derivation to hide the underlyingrepresentation.IIt presents two interfaces to the world:1. The normal public interface treats HirezTime as an opaqueobject.2. A class derived from it can access the fields of the underlyingtimespec/timeval.CPSC 427a, Lecture 14 15/22Outline Goals Stopwatch HangmanPrinting a HirezTime numberSomething seemingly simple like printing HirezTime values is notso simple. Naively, one might


View Full Document

Yale CPSC 427 - Lecture 14

Download Lecture 14
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 14 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 14 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?