0

I have a module called preparation.py which verifies the arguments passed to a function, if it isn't present, instead of using a pre-stabilished value as a keyword argument, it searches the argument as an attribute of an object. The code is the following:

def prep(argslist, argsprovided, attributes):
        argsout = []
        for name in argslist:
            if name in argsprovided:
                argsout.append(argsprovided[name])
            else:
                argsout.append(getattr(attributes,name))

        return argsout

A simple use of this would be:

import preparation as prep
class example(object):
    def __init__(self,x,y):
        self.x = x
        self.y = y

E = example(1,1)

def foo(**kwargs):
    [x,y] = prep.prep(['x','y'],kwargs,E)
    return x + y

print( foo())
print( foo(x = 2))

Since almost every function in my code does this check everytime it's called, I want to know if the time spent on it is considerable. The time spent on a single time when this module is called can't be measured using time.time() method, so I just can't sum a bunch of smaller intervals. Is there a way of doing this?

RFiischer
  • 41
  • 7
  • 2
    The common way to do this is to profile your script. See [_How can you profile a script?_](http://stackoverflow.com/questions/582336/how-can-you-profile-a-script). – martineau Jan 27 '17 at 17:55
  • Possible duplicate of [How can you profile a script?](http://stackoverflow.com/questions/582336/how-can-you-profile-a-script) – Prune Jan 27 '17 at 17:58
  • Python also has a built-in `timeit` module which might help. but it's more for _comparing_ the execution times of different snippets of code. If you want finer-grained information that than the `profile` module provides, check out the [line_profiler](https://pypi.python.org/pypi/line_profiler) add-on module. – martineau Jan 27 '17 at 18:02

0 Answers0