9

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?

1
  • 2
    What kind of task?
    – Nathan W
    Commented Nov 28, 2012 at 5:52

2 Answers 2

6

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():
  startTimes=dict()
  stopTimes=dict()

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

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

  @staticmethod
  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))
3
  • 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
    Commented Nov 28, 2012 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. Commented Nov 28, 2012 at 9:00
  • opps yeah I meant startTimes = {} which is the same as dict(), sorry about that
    – Nathan W
    Commented Nov 28, 2012 at 9:05
3

you can use python timeit function for testing it.

import timeit

def myfunction():
    try:
       #do something
    except AttributeError:
       pass

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

>>0.34454334343 

i hope it helps you....

1
  • 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
    Commented Nov 28, 2012 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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