2014-10-03 41 views
0

我想使用python模块timeit来为我的QGIS插件中的某些功能定时。qgis插件的Timeit模块

在这里,我调用了函数内的函数,我在最后一个函数结束时调用它。不过,看起来这个插件的运行时间比平常要长,我想知道我是否在错误的地方调用了定时器。有没有更好的方法来设置它?

class myPluginName: 

    def firstFunction(self): 
     ... 
     self.secondFunction() 

    def secondFunction(self): 
     ... 
     self.timeThings() 

    def run(self): 
     self.firstFunction() 

    def timeThings(self): 
     QMessageBox.information(None, 'First Function', 'Time : %s' % timeit.timeit(self.firstFunction,number=1) 
     QMessageBox.information(None, 'Second Function', 'Time : %s' % timeit.timeit(self.secondFunction,number=1) 

更新:在遵循一些建议后,我试图用以下方式实现包装。我得到不过,TypeError: firstFunction() takes exactly 1 argument (2 given) on ret = func(**args, **kwargs)

def time_func(func): 
    try: 
     name = func.__name__ 
    except: 
     name = func.f__name 
    def tf_wrapper(*args, **kwargs): 
     t = time.time() 
     ret = func(*args, **kwargs) 
     QMessageLog.logMessage("{}: {}".format(name, time.time() - t)) 
     return ret 
    return tf_wrapper 

class myPlugin: 
    def initGui(self): 
     QObject.connect(self.dlg.ui.comboBox,SIGNAL("currentIndexChanged(int)"), self.firstFunction) 
    @time_func 
    def firstFunc(self): 
     registry = QgsMapLayerRegistry.instance() 
     firstID = str(self.dlg.ui.firstCombo.itemData(self.dlg.ui.firstCombo.currentIndex())) 
     secondID = str(self.dlg.ui.secondCombo.itemData(self.dlg.ui.secondCombo.currentIndex())) 
     self.firstLayer = registry.mapLayer(firstID) 
     self.secondLayer = registry.mapLayer(secondID) 

    @time_func 
    def secondFunc(self): 
     ... 
     self.thirdFunc() 
    def thirdFunct(self): 
     ... 
    def run(self): 
     self.dlg.ui.firstCombo.clear() 
     self.dlg.ui.secondCombo.clear() 
     for layer in self.iface.legendInterface().layers(): 
      if layer.type() == QgsMapLayer.VectorLayer: 
       self.dlg.ui.firstCombo.addItem(layer.name(), layer.id()) 
       self.dlg.ui.secondCombo.addItem(layer.name(), layer.id()) 
     result = self.dlg.exec_() 
     if result == 1: 
      self.secondFunction() 
+0

@matsjoyce我打电话在几个不同的方式多种功能。第一种是在组合框索引中调用。第二个是在对话框上有一个接受按钮。我也有从函数内部调用的函数。查看修改。 – user25976 2014-10-07 20:23:23

+0

@matsjoyce我刚添加了更多的代码 – user25976 2014-10-08 19:07:49

+0

查看我的加入 – matsjoyce 2014-10-08 21:03:14

回答

0

OK,我不知道您的具体情况,但我会设置它虽然装饰:

import time 

def time_func(func): 
    try: 
     name = func.__name__ 
    except: 
     name = func.f_name 
    def tf_wrapper(*args, **kwargs): 
     t = time.time() 
     ret = func(*args, **kwargs) 
     print("{}: {}".format(name, time.time() - t)) # Or use QMessageBox 
     return ret 
    return tf_wrapper 

class myPluginName: 
    @time_func 
    def firstFunction(self): 
     pass 

    @time_func 
    def secondFunction(self): 
     pass 

    def run(self): 
     self.firstFunction() 
myPluginName().firstFunction() 

有了这个代码,任何函数包裹在time_func将会花费时间来执行返回时打印的函数以及其名称。例如。运行它将打印:

firstFunction: 1.430511474609375e-06 

对于您的TypeError,您需要更改;

def firstFunction(self): 
     pass 

要:

def firstFunction(self, index): 
     pass 
+0

谢谢你指出我错误的原因。不过,我想知道,如果这是设置。在函数本身中调用timit函数会更好吗?我现在要更新我的问题,因为我已经绕过了最初的错误。 – user25976 2014-10-03 21:49:42

+0

@ user25976我也更新了。 – matsjoyce 2014-10-04 08:08:40

+0

这看起来就像我正在寻找的东西,但它对我来说有点新鲜。我得到一个TypeError:myFunction()在行ret = func(* args,** kwargs)上只需要1个参数(给出2) – user25976 2014-10-07 18:23:38