180 likes | 351 Views
Timing. Tutorial #5 CPSC 261. Clocks. Most computer systems have a number of clocks Give time signals to the pipeline and memory Tick at 2.67 GHz (or whatever) Interrupt generating clocks Interrupt every X ms Real time clocks Keep track of time in the “real world”. Linux notion of time.
E N D
Timing Tutorial #5 CPSC 261
Clocks • Most computer systems have a number of clocks • Give time signals to the pipeline and memory • Tick at 2.67 GHz (or whatever) • Interrupt generating clocks • Interrupt every X ms • Real time clocks • Keep track of time in the “real world”
Linux notion of time • time() • return the number of seconds since Jan 1, 1970 UTC • gettimeofday() • More fine-grained notion of time • Seconds and milliseconds • Stored in a structure called a timeval
How to measure elapsed time? int before = time(0) // do the work int after = time(0) int elapsed = after – before
Problems with using time • Coarse granularity • Often get an answer of 0 • Sometimes get 1 second even for short operations
Try #2 measuring elapsed time structtimeval before, after; gettimeofday(&before) // do the work gettimeofday(&after) elapsed= after - before
Problems with using gettimeofday • Overhead of measurement may dwarf small elapsed times • structtimevals don’t support subtraction (so you have to write it)
General problems with timing • Timing short-lived events • Repeat them a lot of times • External factors can affect measurement • Other processes • Hardware interrupts • Repeat measurements a few times
Look at timenothing.c • Explain all the interesting bits
Special files • Linux has a number of special files in /dev • null • Reads return EOF • Writes disappear into the bit-bucket • zero • Reads return 0 bytes • Writes disappear into the bit-bucket • random • Reads return random bytes • Writes affect later reads in a non-obvious way
Throwing stuff away • From the command line: • chatty-program > /dev/null
System calls and library calls • The lowest level of interaction with the Linux system is via system calls • read, write, open, close • Documented in section 2 of the manual • More “user-friendly” interfaces can often be found in the “standard library” • fread, fwrite, fopen, fclose • Documents in section 3 of the manual
issawtooth • issawtooth is a function that detects sawtooth patterns • Each value is alternatively bigger or smaller than the one before it • Starting with bigger issawtooth(1) == 1 issawtooth(1, 4, 3, 10, 3, 5) == 1 issawtooth(1, 4, 4, 10,3, 5) == 0 issawtooth(1, 4, 5, 10, 3, 5) == 0
Look at issawtooth.c long issawtooth(long *p, long n) { long i; long up = 1; for (i = 1; i < n; i++) { if (up && p[i] <= p[i-1]) return 0; if (!up && p[i] >= p[i-1]) return 0; up = !up; } return 1; }
Look at issawtooth.s • Explain what it is doing
Make it faster • Less memory accesses • Less branches • Less dependencies
The online scoreboard • Each time you check in your lab, the scoreboard will be updated • Your checkin will be a bit slower • https://www.ugrad.cs.ubc.ca/~cs261/2013w2/scoreboard/index.php