[CentralOH] One Change Too Far: Got Rid of Temporary Variable
jep200404 at columbus.rr.com
jep200404 at columbus.rr.com
Sat Oct 21 22:50:55 EDT 2017
On Sat, 21 Oct 2017 01:11:18 -0400, jep200404 at columbus.rr.com wrote:
> set c [expr $a + $b]
> set a $b
> set b $c
Below: One change too far:
Get rid of the temporary variable c that is above.
It is interesting that it can be done at all.
But golly, it sure is hard to understand how the program works now.
The extra calculation also makes the program slower
than the one that used the temporary variable.
Also uses shebang.
doj at sbc:~/20171020$ cat fib-without-c.tcl
#!/usr/bin/env tclsh
proc fib {n} {
set a 0
set b 1
for {set i 0} {$i < $n} {incr i} {
set b [expr {$a + $b}]
set a [expr {$b - $a}]
}
return $a
}
puts "Please enter seed value:"
gets stdin x
set StartTime [clock microseconds]
set Answer [fib $x]
set EndTime [clock microseconds]
puts "Seed $x; Result $Answer"
puts "Duration [expr $EndTime - $StartTime] microseconds"
doj at sbc:~/20171020$
doj at sbc:~/20171020$ diff fib-with-braces.tcl fib-without-c.tcl
7,9c7,8
< set c [expr {$a + $b}]
< set a $b
< set b $c
---
> set b [expr {$a + $b}]
> set a [expr {$b - $a}]
doj at sbc:~/20171020$
doj at sbc:~/20171020$ echo 11111 | time ./fib-without-c.tcl
Please enter seed value:
Seed 11111; Result 515...489
Duration 81068 microseconds
0.11user 0.00system 0:00.11elapsed 97%CPU (0avgtext+0avgdata 4940maxresident)k
0inputs+0outputs (0major+356minor)pagefaults 0swaps
doj at sbc:~/20171020$
More information about the CentralOH
mailing list