1

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:

  • a) when does compilation time become significant enough for such differences to matter?
  • b) How might I go about writing my own profiler that takes into account compilation time?
Leah Xue
  • 625
  • 1
  • 7
  • 11

2 Answers2

4

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).

gaborous
  • 15,832
  • 10
  • 83
  • 102
1

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.

agf
  • 171,228
  • 44
  • 289
  • 238
  • ack, sorry, what I meant by my comment was that -- I recognize cProfile and Profile subtract compilation time from actually running time. I am going to edit the post now. – Leah Xue Aug 20 '11 at 09:14
  • I also answer your question about what `profile` does that your version doesn't do. It tells you line by line how much time your function took, how much time it took doing each individual thing, not just the total running time. – agf Aug 20 '11 at 09:22