简体   繁体   English

用于Qgis插件的Timeit模块

[英]Timeit module for qgis plugin

I'd like to use the python module timeit to time some functions in my QGIS plugin. 我想使用python模块timeit来计时QGIS插件中的某些功能。

Here, I've called the time it function within a function that I call at the end of the last function. 在这里,我在最后一个函数结尾处调用的函数中调用了时间函数。 It seems, though, that the plugin is taking even longer to run than usual and I am wondering if i'm calling the timer in the wrong place. 但是,似乎该插件比平常需要更长的时间运行,我想知道我是否在错误的地方调用了计时器。 Is there a better way to set this up? 有没有更好的方法来进行设置?

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)

UPDATE: After following some advice, i've tried to implement the wrapper in the following way. 更新:遵循一些建议后,我尝试通过以下方式实现包装器。 I get however, a TypeError: firstFunction() takes exactly 1 argument (2 given) on ret = func(**args, **kwargs) 但是我得到一个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()

OK, I don't know your exact situation, but I'd set it up though decorators: 好的,我不知道您的确切情况,但是我将通过装饰程序进行设置:

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

With this code, any function wrapped in time_func will have the time taken to execute the function printed when it returns, along with its name. 使用此代码,包装在time_func任何函数将在返回时执行执行该函数所花费的时间及其名称。 Eg running it will print: 例如运行它将打印:

firstFunction: 1.430511474609375e-06

For your TypeError , you need to change; 对于您的TypeError ,您需要进行更改;

    def firstFunction(self):
        pass

To: 至:

    def firstFunction(self, index):
        pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM