Thursday, 7 June 2018

Profile small code snippets

Had a requirement to test some code that was causing timing problems, so looked at a few ways that this may be achieved and decided on timeit a default python language package.

import timeit
     
t = timeit.Timer("def test(): print("def test()", "print('setup')")

print(t.timeit(10000))

print(t.repeat(3,10000))
       

In my code I use the following no requirement for setup so empty, just call the API method that I want to time.

import timeit

t = timeit.Timer("def example(): x10i.CachedReadAndResetSecuritySwitchFlags()", "")
print(t.timeit(10000))
print(t.repeat(3,10000))
       

And that's it simple!

No comments:

Post a Comment

Classes

My favourite subject, seriously! A simple class class MyClass(): pass There are two types of class variables, class variables and Inst...