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

Computer Science & Information Technology

You might also like to view...

A ____ is an item that you create in Outlook to track until its completion.

A. schedule B. project C. list D. task

Computer Science & Information Technology

In Impress, animations should be used to impress the audience

Indicate whether the statement is true or false

Computer Science & Information Technology

Another name for a data macro is an update macro

Indicate whether the statement is true or false

Computer Science & Information Technology

Flash Fill cannot be used when worksheets are grouped

Indicate whether the statement is true or false

Computer Science & Information Technology