Rewrite the program while_1 (page 405) so that it runs faster. Use the time builtin to verify the improvement in execution time.
What will be an ideal response?
Speed things up by adding the –f option to the line that calls tcsh to avoid
calling ~/.tcshrc:
$ cat while_2
#!/bin/tcsh -f
...
$ time while_1 1000
The sum is 500500
0.105u 0.083s 0:00.19 94.7% 0+0k 0+0io 0pf+0w
$ time while_2 1000
The sum is 500500
0.092u 0.055s 0:00.14 100.0% 0+0k 0+0io 0pf+0w
You can avoid the while loop altogether by using an equation to speed up
the calculation:
$ cat while_4
#!/bin/tcsh -f
@ sum = ($argv[1] * $argv[1] + $argv[1]) / 2
echo "The sum is $sum"
$ time while_4 1000
The sum is 500500
0.000u 0.002s 0:00.00 0.0% 0+0k 0+0io 0pf+0w
You might also like to view...
How will the spreadsheet model help management make better decisions? How would the business benefit from being able to simulate information on a regular basis?
What will be an ideal response?
What function would you use to generate a random number to simulate the roll of a six-sided die?
A. nextBooleanInRange(1, 6) B. nextIntegerFromAToBExclusive(1, 6) C. nextIntegerInRange(1, 6) D. nextIntegerFromAToBInclusive(1, 6)