| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 79
- TURBO-LESSON 17: A TIMING FUNCTION
- OBJECTIVES - In this lesson, you will learn about:
- 1. Include files
- 2. A timing function
- 3. Improving timing accuracy
- 4. Sources of errors in timing
- 1. Include files.
- After you have debugged and tested a subprogram, you no longer
- need to see the source statements anymore. It would be great if
- you could just use the subprogram like one of the predefined
- functions or procedures.
- You can, almost!
- Here's how:
- (1) Store the subprogram in a file. You may want to use a
- filename extension such as .INC to mark your "Include" files.
- (2) In the program where you will use the subprogram, use the
- special comment statement to include the file. It's form is:
- {$I ABC.INC }
- How it works:
- As the program is compiling, the contents of the file, ABC.INC
- will be compiled at the point where {$I ABC.INC } occurs in the
- program.
- ##### DO:
- Look at PROG17. Notice the statement
- {$I Time.Inc Get System time in seconds }
- This statement instructs the compiler to compile whatever it
- finds in a file named Time.Inc.
- The actual function, Time, is not in PROG17. All that appears in
- PROG17 is the include statement. Include files provide a way to
- keep your programs uncluttered.
- Œ
- TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 80
- ##### DO:
- Inspect Time.Inc.
- For now, just note that the contents of this file are program
- statements that can be compiled.
- ##### DO:
- Compile PROG17. Watch the bottom left of the screen.
- How do you know when the include file is being compiled?
- Did you see the "I"? If not, compile the program again.
- 2. A timing function.
- The include file, Time.Inc, contains a function called Time which
- provides the time from the system clock.
- Since this is an include file, you don't have to know how it
- works, just what it does. It can be viewed like a predefined
- function.
- The function provides the system clock time converted to seconds.
- This makes it easy to compare two times by subtracting the
- earlier time from the later.
- ##### DO:
- Examine PROG17 again.
- PROG17 gets Start_Time from the function, Time.
- A FOR loop is used to repeat something you want to time.
- After the FOR loop is done, Stop_Time is obtained from Time.
- The results printed give both total elapsed time and the time for
- a single repetition.
- ##### DO:
- Run PROG17 using 1000 as the number of repetitions.
- The function, Time, is itself being timed in PROG17.
- Œ
- TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 81
- ##### DO:
- Run the program with the 10 as the number of repetitions. Input
- 10 several times and compare the output.
- ##### DO:
- Run the program inputting 1 several times.
- Do you think the time function is accurate?
- 3. Improving Timing Accuracy.
- One way to improve the timing accuracy, in this case, is to
- average the time over many repetitions.
- ##### DO:
- Run the program to complete the table below.
- Number of Time for
- Repetitions One Repetition
- 10
- 50
- 100
- 500
- 1000
- 2000
- 5000
- What conclusions can you make from the results you observed?
- What is the minimum number of repetitions needed for reasonably
- accurate results?
- ##### DO:
- Run the program, inputting the "minimum" you chose above several
- times. Are the results accurate?
- Try it again with a minimum about half as large. Are the results
- less accurate?
- Œ
- TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 82
- 4. Sources of errors in timing.
- Time to look at another problem!
- What is actually being timed in PROG17?
- The statement (A:=Time;)?
- The timing section of PROG17 follows:
- Start_Time := Time;
- FOR I := 1 to K DO
- A := Time;
- Stop_Time := Time;
- What period of time is actually included in Elapsed_Time?
- The period measured starts at the end of the execution of the
- statement: Start_Time := Time;
- and ends at the end of the execution of the statement:
- Stop_Time := Time;
- This includes the time needed to execute
- FOR I := 1 to K DO
- and Stop_Time := Time;
- in addition to the time for the action we want to measure:
- A := Time;
- Is this a serious problem?
- ##### DO:
- Run the program for 1000 repetitions.
- Record the total elapsed time and the time for one repetition for
- later comparison.
- Œ
- TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 83
- ##### DO:
- Change the FOR statement to:
- FOR I := 1 to K DO; {Do nothing. Semicolon ends the FOR loop.}
- Delete or comment out the statement, (A := Time;).
- Run the program for 1000 repetitions.
- Record the total elapsed time and the time for one repetition.
- These are times with the (A := Time;) statement removed.
- How do these times compare to the times recorded in the previous
- exercise?
- Are these times at least 10 times as small as the times in the
- previous exercise?
- Do you think you are getting a reasonable measure of the Time
- function?
- ##### DO:
- Change the FOR statement back to its original form:
- FOR I := 1 to K DO
- After the FOR statement, add the statement:
- J := 2;
- Run the program for 1000 repetitions.
- Record the results. How do they compare with the times for the
- FOR loop?
- Do you know how much time is needed to execute (J := 2;)?
- ##### DO:
- Try to formulate a rule to help you decide whether you are
- getting reasonable results in a particular case using the Time
- function.
- Œ
|