[CentralOH] 2017-10-20 燕月 Lunch Placemat Scribbles: Simplified Code Making It More Readable

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Sat Oct 21 01:11:18 EDT 2017


On Fri, 20 Oct 2017 22:24:22 -0400, jep200404 at columbus.rr.com wrote:

>     tcl braces
>     versus python
>     fibonacci and hamming numbers

I simplified the tcl and Python fibonacci programs.
Readability was improved.

    (jupy) doj at sbc:~/20171020$ cat fib-no-braces.tcl
    proc fib {n} {
       set a 0
       set b 1
       for {set i 0} {$i < $n} {incr i} {
          set c [expr $a + $b]
          set a $b
          set b $c
       }
       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"
    (jupy) doj at sbc:~/20171020$ 
    (jupy) doj at sbc:~/20171020$ diff FibForXNoBraces.tcl fib-no-braces.tcl
    1,16c1,7
    < proc Fib {n} {
    < 
    <    if {$n == 0} {
    <       set Ret_Value 0
    <    } else {
    <       if {$n < 3} {
    <          set Ret_Value 1
    <       } else {
    <          set PrevVal 1
    <          set ThisVal 1
    <          for {set y 3} {$y <= $n} {incr y} {
    <             set Ret_Value [expr $PrevVal + $ThisVal]
    <             set PrevVal $ThisVal
    <             set ThisVal $Ret_Value
    <          }
    <       }
    ---
    > proc fib {n} {
    >    set a 0
    >    set b 1
    >    for {set i 0} {$i < $n} {incr i} {
    >       set c [expr $a + $b]
    >       set a $b
    >       set b $c
    18,20c9
    < 
    <    return $Ret_Value
    < 
    ---
    >    return $a
    28c17
    < set Answer [Fib $x]
    ---
    > set Answer [fib $x]
    (jupy) doj at sbc:~/20171020$ 

    ###########################################################################

    (jupy) doj at sbc:~/20171020$ cat fib-with-braces.tcl
    proc fib {n} {
       set a 0
       set b 1
       for {set i 0} {$i < $n} {incr i} {
          set c [expr {$a + $b}]
          set a $b
          set b $c
       }
       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"
    (jupy) doj at sbc:~/20171020$ 
    (jupy) doj at sbc:~/20171020$ diff fib-no-braces.tcl fib-with-braces.tcl
    5c5
    <       set c [expr $a + $b]
    ---
    >       set c [expr {$a + $b}]
    (jupy) doj at sbc:~/20171020$ 

    ###########################################################################

    (jupy) doj at sbc:~/20171020$ cat fib2.py 
    #!/usr/bin/env python3

    import sys
    from timeit import timeit

    source_code = '''
    def fib(n):
        a, b = 0, 1
        for _ in range(n):
            a, b = b, a + b
        return a
    '''

    exec(source_code)

    def main():
        for arg in sys.argv[1:]:
            n = int(arg)
            number = 1000
            elapsed_time = timeit('fib(%s)' % n, setup=source_code, number=number)
            print(
                f'{elapsed_time}s total / {number} loops -> '
                f'{elapsed_time / number}s per loop for fib({n}) -> {fib(n)}'
            )

    if __name__ == '__main__':
        main()
    (jupy) doj at sbc:~/20171020$ diff fib.py fib2.py
    8,9d7
    <     if n == 0:
    <         return 0
    (jupy) doj at sbc:~/20171020$ 

    ###########################################################################

As expected, those changes had negligible effects on the exection times.

    (jupy) doj at sbc:~/20171020$ echo 11111 | tclsh fib-no-braces.tcl
    Please enter seed value:
    Seed 11111; Result 515...489
    Duration 129060134 microseconds
    (jupy) doj at sbc:~/20171020$ 

    ###########################################################################

    (jupy) doj at sbc:~/20171020$ echo 11111 | tclsh fib-with-braces.tcl
    Please enter seed value:
    Seed 11111; Result 515...489
    Duration 43387 microseconds
    (jupy) doj at sbc:~/20171020$ 

    ###########################################################################

    (jupy) doj at sbc:~/20171020$ ./fib2.py 0 1 2 3 4 5 6 7 11111
    0.0012247690001458977s total / 1000 loops -> 1.2247690001458977e-06s per loop for fib(0) -> 0
    0.0017716410002321936s total / 1000 loops -> 1.7716410002321935e-06s per loop for fib(1) -> 1
    0.0018954030001623323s total / 1000 loops -> 1.8954030001623324e-06s per loop for fib(2) -> 1
    0.002083211000353913s total / 1000 loops -> 2.0832110003539127e-06s per loop for fib(3) -> 2
    0.002242174000457453s total / 1000 loops -> 2.242174000457453e-06s per loop for fib(4) -> 3
    0.002450655999382434s total / 1000 loops -> 2.450655999382434e-06s per loop for fib(5) -> 5
    0.002553395000177261s total / 1000 loops -> 2.5533950001772608e-06s per loop for fib(6) -> 8
    0.003030005000255187s total / 1000 loops -> 3.030005000255187e-06s per loop for fib(7) -> 13
    8.490469498000493s total / 1000 loops -> 0.008490469498000494s per loop for fib(11111) -> 515...489
    (jupy) doj at sbc:~/20171020$ 


More information about the CentralOH mailing list