Python Forum
code timer - PyTimer - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: code timer - PyTimer (/thread-1208.html)



code timer - PyTimer - JacobMorris - Dec-13-2016

PyTimer is a module that I created meant to make calculating code execution times much easier. Previously, the only real option was to calculate the time before, then after, and subtract the two, and that had to be done for every block of code you were timing. PyTimer allows the time at specific parts to be logged easily, and then the values can all be displayed easily.

An example:

timer = PyTimer()

for run in range(100):
   temp = ""
   for i in range(10000):
       temp += str(i)
   timer.log()

timer.split("Concat W/ Variable")

for run in range(100):
   temp = []
   for i in range(10000):
       temp.append(str(i))
   "".join(temp)
   timer.log()

timer.split("Concat W/ List & .join")
timer.display_averages()
Output:
Concat W/ Variable:    Average (100 runs): 0.0045 s Concat W/ List & .join:    Average (100 runs): 0.0035 s
Some neat features of this add-on are:
  • Functions can be timed using a decorator
  • Strings of code can be evaluated
  • Functions can be timed over multiple iterations
  • The timer supports splitting
Links:


RE: code timer - PyTimer - Mekire - Dec-13-2016

(Dec-13-2016, 04:12 PM)JacobMorris Wrote: Previously, the only real option was to calculate the time before, then after, and subtract the two
Not to take away from what you have done, but are you aware of the standard library module timeit?


RE: code timer - PyTimer - JacobMorris - Dec-13-2016

(Dec-13-2016, 04:39 PM)Mekire Wrote:
(Dec-13-2016, 04:12 PM)JacobMorris Wrote: Previously, the only real option was to calculate the time before, then after, and subtract the two
Not to take away from what you have done, but are you aware of the standard library module timeit?

I am in fact, but, in my opinion, timeit is missing a lot of features. It works great if you are timing functions or code that is in a string, but if you want to check how long a section of code takes to run that isn't in a function, then you are basically left with using timeit.default_timer() twice, and doing the subtraction yourself. It appears to me that the use case for timeit is meant more for use in a command line environment, or in IDLE, and less for use in a larger project.