【问题标题】:Timeit module for qgis pluginqgis 插件的 Timeit 模块
【发布时间】:2014-10-09 05:02:35
【问题描述】:

我想使用 python 模块 timeit 来为我的 QGIS 插件中的一些功能计时。

在这里,我在最后一个函数末尾调用的函数中调用了 time it 函数。不过,该插件的运行时间似乎比平时更长,我想知道我是否在错误的地方调用了计时器。有没有更好的设置方法?

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

【问题讨论】:

  • @matsjoyce 我以几种不同的方式调用多个函数。第一个是通过更改组合框索引来调用的。第二个是对话框上的接受按钮。我也有从函数内部调用的函数。查看修改。
  • @matsjoyce 我刚刚添加了更多代码

标签: python qgis


【解决方案1】:

好的,我不知道你的确切情况,但我会通过装饰器进行设置:

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

【讨论】:

  • 感谢您指出我的错误原因。不过,我想知道,如果这是设置。如果在函数本身期间调用 timit 函数会更好吗?既然我已经被初始错误绕过了,我将稍微更新一下我的问题。
  • 这看起来就像我正在寻找的东西,但对我来说有点新。我得到一个 TypeError: myFunction() 在 ret = func(*args, **kwargs)
  • @user25976 太奇怪了……你怎么称呼它的?
猜你喜欢
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 2013-09-23
相关资源
最近更新 更多