Nash - Scientific Computing with PCs (523165), страница 25
Текст из файла (страница 25)
When the measureis smaller than some tolerance, the program stops. By making tolerances larger, the program will usually(but not always!) stop earlier, but the results may not be as "good". The difficulty is in finding ormodifying software to get the right time and accuracy.To obtain a "low accuracy" method, we need to understand how accurate the solution must be, thenchoose or adapt a method, possibly quite crude to achieve it.
A simple example is the computation of thecumulative Normal (Gaussian) distribution by numerical integration. By increasing the number ofintegration points, we hope to increase the accuracy of the approximation to cumulative probability. Notethat increasing the number of integration points does not necessarily increase the accuracy of the result,since there may be rounding errors in summing many small numbers.
Table 8.5.1 shows a sample ofresults in both 8 and 14 digit arithmetic from a now-defunct North Star Horizon. The decision to use 8or 14 digits is itself a time / accuracy tradeoff. Here, we also need more array space for more integrationpoints, though the program can be arranged to need only a few storage locations independent of thenumber of integration points.Table 8.4.2 Time penalty for file fragmentation as found by linear least squares regression of the time toread one line of a file versus the number of file fragments. We use R-squared as a measure of thesuccess of linear model, with 1 indicating a perfect fit, 0 showing that fragments do not help toexplain the read time.
We give the t-statistic of the coefficient of "fragments". An absolute value < 1for the t-statistic indicates that it may not be worth using fragments to model the read time.3.5" FlexibleFixed diskRAM diskFile of 720 linesR-squared t(fragments)0.821717.5710.08334.2430.00050.221File of 120 linesR-squared t(fragments)0.23655.5100.1131-5.0240.0241-1.55670Copyright © 1984, 1994 J C & M M NashSCIENTIFIC COMPUTING WITH PCsNash Information Services Inc., 1975 Bel Air Drive, Ottawa, ON K2C 0X1 CanadaCopy for:Dr.
Dobb’s JournalTable 8.5.1 Computing the probability that the standard Normal distribution has an argument less than1.960 by integration. Times are given in seconds. Abramowitz and Stegun (1965) give a valueof 0.97500210485178.Number ofintegrationpoints2481664128256512102420488.68 digit FPBASICvaluetime0.948235550.972848070.974803060.974981010.975001820.975002080.975001870.975002080.9750022300.10.20.41.63.26.412.925.814 digit FPBASICvaluetime0.94823556024020.97284808529680.974803045862960.974981000289430.975001814133670.9750020692706100.10.20.41.93.90.975002104304760.975002104783520.9750021048436715.831.563.3Timing toolsWe strongly recommend measuring the performance of programs as they are actually used.
Experts canonly give opinions about performance. What count are test results. We urge users to watch for simplechanges, such as using a RAM-disk for data input or disk cache for output, which can lead to dramaticspeed improvements with minimal effort from the user. How then, should users conduct the timings?When we started using PCs, we commonly used a stopwatch. This is reliable, but not very accurate forprograms that run quickly. Fortunately, most computers now have usable clocks. The main worry is thatthe "tick" in many PCs is quite slow, typically about 1/20 of a second, even though times may be reportedto several decimal places.
We do not trust the timing of intervals of less than 1/4 second unless sure thatthe clock and the software to access it are capable of fine-resolution timings.The software mechanisms to access clocks are a serious nuisance for programmers. The sub-programsprovided are not standardized, so that timing of the same routine with several compilers may require usto alter the code for each compiler. Worse, the timing resolution may vary between compilers.Occasionally we may have to use the "time-of-day" clock functions, giving only 1 second timing resolution.Sometimes the time-of-day is provided as a string variable, which makes for more work.A sub-program that returns elapsed time in seconds or hundredths of seconds since some starting pointis wanted.
We note, as did Cinderella, that strange things happen at midnight. A negative timing inChapter 18 was corrected by adding 24*60*60 = 86,400 seconds.A few programming environments provide no mechanism for accessing the clock. We can consider writingassembly or machine code routines to get the time. We did this some years ago for our North StarHorizon, although the manufacturer said such a program was not possible, and even spent a pleasanthour or so at the computing center of the Technical University of Vienna in 1980 showing others how todo it.
There is a satisfaction in overcoming such programming obstacles, but only if the timing routinesare used regularly can the effort be justified.An alternative is to return to the idea of a stopwatch and build this into our computing environment. Wefind our MS-DOS program TIMER, for which partial PASCAL source code is given in Figure 8.6.1, to bevery useful. This program "runs" the program we wish to time and gives us a report of the total elapsedtime for the run. During the elapsed interval, we must load and exit from the program we are timing, soit is a good idea to work from a RAM-disk. Alternatively we can compile a "dummy" version of the8: TIMING CONSIDERATIONS71program in which the first executable statement is replaced by a STOP or similar command, so that wecan time the program load.
(Note that the rest of the code should be kept to give the load module theproper size.) Clearly, any statements that require input from the keyboard are also going to be timed. Thisproblem can be overcome by arranging that all input comes from a file. Sometimes we can useinput/output redirection to get data from a file. As an example, to time the program MYCOMPN.EXEusing data in a file MYCOMPN.DTA, with output sent to a file MYCOMPN.OUT, we would issue thecommandTIMER MYCOMPN.EXE <MYCOMPN.DTA >MYCOMPN.OUTAgain, the default ("logged-in") disk drive containing the program and input files should be a RAM-diskto minimize the data read times.When using computational or statistical packages, such as MATLAB or Stata, we cannot use this approachif we want to time how long it takes to run particular sets of commands within the package. Somepackages provide timing commands, e.g., MATLAB offers a wall clock and a routine for using this tocompute elapsed times in seconds. With other packages we may need to invoke operating systemcommands, e.g., in Stata we can preface an operating system command with an exclamation point andhave it executed from within the package.
With some ingenuity, one may be able to use such mechanismsto save a time stamp in a form readable by the package itself and thereby develop ways to time processesautomatically. Lacking timing tools or a method for accessing external programs, we must get out thestopwatch.All timings suffer from the possibility that events too fast for our "stopwatch" to register — everythinghappens between clock ticks. To overcome this, we put operations to be timed inside a loop. This is trivialto arrange, but we must then exclude administrative overheads of program startup or looping from ourtimings.
As before, the answer is to develop "dummy" versions of programs that have the parts we wishto time commented out. The desired timing is then obtained as the difference between the timings for the"active" and "dummy" programs.Sometimes we wish to compare performance of programs with or without numeric co-processors. Someauthors claim the only sure way to "turn off" the co-processor is to unplug it.
However, sometimes wecan use operating system environment variables. Table 8.6.1 shows some settings for MS-DOS compilers.These control settings may not be listed in the index of a manual, or may be listed under headings we,the users, are unlikely to consider. In fact, we rely on trial and error and the timings to see if they work.This is how we decided that the co-processor could be suppressed for QBASIC 1.0 in Table 8.2.1.72Copyright © 1984, 1994 J C & M M NashNash Information Services Inc., 1975 Bel Air Drive, Ottawa, ON K2C 0X1 CanadaSCIENTIFIC COMPUTING WITH PCsCopy for:Dr. Dobb’s JournalTable 8.6.1 MS-DOS Commands to turn off 80x87 co-processor function for selected compilers. Some arecase sensitive and require appropriate spellings.
The relevant flags are set to null (that is,there are no characters after the equals sign in the SET statement) to restore functionality, e.g.,SET 87=compilersuppress 80x87Borland Turbo BASIC 1.0SET 87=noTurbo Pascal 5.0SET 87=NOvarious Microsoft productsSET NO87=Coprocessor SuppressedFigure 8.6.1Parts of TIMER2.PAS to time the execution of other programs in MS-DOS machines.Copyright J C Nash 1991program Timer2; {Runs other programs and displays the time taken to do so.}{$M 8192,0,0}{ Leave memory for child process }uses Dos;{ To access the timing procedures in TP 5.5 }varCommand, parmstr: string;i, nparm: integer;hr1, hr2, min1, min2, sec1, sec2, hun1, hun2: word;dsec: real;lsec: longint;beginwriteln; writeln(’TIMER2 (C) J.C.Nash 1991 -- execute & time DOS programs’);writeln; nparm:=paramcount; parmstr:=’’;if nparm=0 thenbeginwriteln(’You MUST provide a program to run i.e.
TIMER MYRUN.EXE’);writeln(’Give FULL path if needed, i.e. D:\mydir\mysdir\myprog.exe’);end;begin {at least 1 paramter (the program}command:=paramstr(1);for i:=2 to nparm do parmstr:=parmstr+’ ’+paramstr(i);end;...gettime(hr1, min1, sec1, hun1); dsec:=sec1+0.01*hun1; {start time}SwapVectors; Exec(Command,parmstr); SwapVectors;if (DosError<>0) thenbeginwriteln(’Termination with DosError =’,DosError);if DosError=2 then writeln(’?? wrong path to executable file’);end;writeln(’DosExitCode =’,DosExitCode);writeln(’TIMER2 start ’,hr1:2,’:’,min1:2,’:’,dsec:5:2);gettime(hr2, min2, sec2, hun2); dsec:=sec2+0.01*hun2; {end time}writeln(’TIMER2 end ’,hr2:2,’:’,min2:2,’:’,dsec:5:2);dsec:=0.01*(hun2-hun1)+1.*(sec2-sec1)+60.*(min2-min1+60.*(hr2-hr1));writeln(’Elapsed seconds = ’,dsec:8:2);end.Previous Home Next9: DEBUGGING73Chapter 9Debugging9.19.29.39.49.59.69.79.8Using output wiselyBuilt-in debugging toolsListings and listing toolsSub-testsTest and example dataScripts and documentationResilience to I/O failure and disk problemsTests of language translatorsThe word "debugging" entered the jargon of computers one August night in 1945 when a two-inch mothwas discovered inside the Mark I at Harvard.