Download presentation
Presentation is loading. Please wait.
Published byLilian Small Modified over 9 years ago
1
Timing Tutorial #5 CPSC 261
2
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”
3
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
4
How to measure elapsed time? int before = time(0) // do the work int after = time(0) int elapsed = after – before
5
Problems with using time Coarse granularity Often get an answer of 0 Sometimes get 1 second even for short operations
6
Try #2 measuring elapsed time struct timeval before, after; gettimeofday(&before) // do the work gettimeofday(&after) elapsed = after - before
7
Problems with using gettimeofday Overhead of measurement may dwarf small elapsed times struct timevals don’t support subtraction (so you have to write it)
8
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
9
Look at timenothing.c Explain all the interesting bits
10
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
11
Throwing stuff away From the command line: – chatty-program > /dev/null
12
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
13
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
14
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; }
15
Look at issawtooth.s Explain what it is doing
16
Make it faster Less memory accesses Less branches Less dependencies
17
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 https://www.ugrad.cs.ubc.ca/~cs261/2013w2 /scoreboard/index.php
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.