import time

def time_usage(func):
    def wrapper(*args, **kwargs):
        beg_ts = time.time ()
        func(*args, **kwargs)
        end_ts = time.time ()
        print("elapsed time: %f" % (end_ts - beg_ts))
    return wrapper

"""
Explanation: iPython Cookbook - timing execution
Timing function execution using decorators
We here define a decorator that at the beginning of the execution remembers the time, and at the end of the exexution prints the time elapse. Note that this is wallclock time, not process time, so it will depend on what else is going on at that moment source
End of explanation
"""


@time_usage
def test():
    for i in range(0, 100000):
        pass

"""
Explanation: To always time execution of a function, just add the decorator to its definition...
End of explanation
"""


test()

"""
Explanation: ...and whenever the function is run it will print the time spent in it
End of explanation
"""


import time
def new_timer(id=''):
    beg_ts = time.time ()
    
    def finish():
        end_ts = time.time ()
        print("%s elapsed time: %f" % (id, end_ts - beg_ts))
        return end_ts - beg_ts

    return finish

"""
Explanation: Timing code segments
This function remembers the time when it is called in a closure, and returns a function that allows to read it out.
End of explanation
"""


mytimer = new_timer('TIMERID')
for i in range(0, 100000):
    pass
mytimer()

"""
Explanation: The way it works is as follows
End of explanation
"""


import timeit
timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)

timeit.timeit('"-".join([str(n) for n in range(100)])', number=10000)

timeit.timeit('"-".join(map(str, range(100)))', number=10000)

"""
Explanation: Timing snippets using timeit
For timing the execution time of snippets - eg to compare different ways of programming the same thing - there is a function called timeit in the eponymous module source
End of explanation
"""


import sys
print(sys.version)

"""
Explanation: Licence and version
(c) Stefan Loesch / oditorium 2014; all rights reserved 
(license)
End of explanation
"""


