Leah Xue
Leah Xue

Reputation: 635

What do I gain from using Profile or cProfile

versus something like this:

def time_this(func):
    @functools.wraps(func)
    def what_time_is_it(*args, **kwargs):
        start_time = time.clock()
        print 'STARTING TIME: %f' % start_time
        result = func(*args, **kwargs)
        end_time = time.clock()
        print 'ENDING TIME: %f' % end_time
        print 'TOTAL TIME: %f' % (end_time - start_time)
        return result
     return what_time_is_it

I am asking because writing a descriptor likes this seems easier and clearer to me. I recognize that profile/cprofile attempts to estimate bytecode compilation time and such(and subtracts those times from the running time), so more specifically.

I am wondering:

Upvotes: 2

Views: 2620

Answers (2)

gaborous
gaborous

Reputation: 16590

Profile is slower than cProfile, but does support Threads.

cProfile is a lot faster, but AFAIK it won't profile threads (only the main one, the others will be ignored).

Upvotes: 5

agf
agf

Reputation: 176780

Profile and cProfile have nothing to do with estimating compilation time. They estimate run time.

Compilation time isn't a performance issue. Don't want your code to be compiled every time it's run? import it, and it will be saved as a .pyc, and only recompiled if you change it. It simply doesn't matter how long code takes to compile (it's very fast) since this doesn't have to be done every time it's run.

If you want to time compilation, you can use the compiler package.

Basically:

from timeit import timeit
print timeit('compiler.compileFile(' + filename + ')', 'import compiler', number=100)

will print the time it takes to compile filename 100 times.

If inside func, you append to some lists, do some addition, look up some variables in dictionaries, profile will tell you how long each of those things takes.

Your version doesn't tell you any of those things. It's also pretty inaccurate -- the time you get depends on the time it takes to look up the clock attribute of time and then call it.

If what you want is to time a short section of code, use timeit. If you want to profile code, use profile or cProfile. If what you want to know is how long arbitrary code took to run, but not what parts of it where the slowest, then your version is fine, so long as the code doesn't take just a few miliseconds.

Upvotes: 2

Related Questions