I am working on QGIS. I need to determine how much time QGIS needed to finish a specific task. I have been unable to get execution times for the processes. What is the proper way to get processing time under QGIS?

  • 2
    What kind of task? – Nathan W Nov 28 '12 at 5:52

What kind of tasks are you referring to?

I use the following static class to benchmark my code.

I designed it to be static so that I can call the start/stop/show methods wherever is needed without worrying about passing references to the Timer back and forth.

The optional parameters allow me to have several running at once, any values will do but names look nicer.

Sometimes its necessary to create a stub method and connect it to a signal in order to call these methods at the right moment, but without knowing more about what you are timing its hard to be specific.

from PyQt4.QtGui import *
from datetime import datetime

class Timer():

  def start(key = 0):
    Timer.startTimes[key] = datetime.now()
    Timer.stopTimes[key] = None

  def stop(key = 0):
    Timer.stopTimes[key] = datetime.now()

  def show(key = 0):
    if key in Timer.startTimes:
      if Timer.startTimes[key] is not None:
        if key in Timer.stopTimes:
          if Timer.stopTimes[key] is not None:
            delta = Timer.stopTimes[key] - Timer.startTimes[key]
            QMessageBox.information(None,"Timer: " + str(key), str(delta))
| improve this answer | |
  • A class with all staticmethods is normally considered bad practice. Just have a timer.py file and have these method as first class, also startTimes can be startTimes = [] – Nathan W Nov 28 '12 at 8:34
  • Point taken, but I wasn't really fussed about best practice when coding this it is very ephemeral in nature. I use it to compare relative performance of alternative implementations and then remove it from the project once a decision is made. startTimes is a dict so I can use strings as keys and provide readable names. – Kelly Thomas Nov 28 '12 at 9:00
  • opps yeah I meant startTimes = {} which is the same as dict(), sorry about that – Nathan W Nov 28 '12 at 9:05

you can use python timeit function for testing it.

import timeit

def myfunction():
       #do something
    except AttributeError:

print timeit.timeit('testfunc(), 'from __main__ import myfunction as testfunc')


i hope it helps you....

| improve this answer | |
  • Thank You for your help. I am working with big datasets, I need to know how long took to process a simple function (i.e. select by location). I know that can be done with python, but I was trying to call a log/ report function. Thanks!!! – user13164 Nov 28 '12 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.