我有一个显示“trait”对象的gui元素的项目。对象在树中,他们知道如何为自己构建用户界面。然后将这些用户界面连接(绑定)到对象。现在我最大的问题是为对象树创建GUI所需的时间。我不能想出一个MWE,因为实际的项目包含了几百个不同的类,并且所有类都是不同的。但我可以在下面复制cProfile统计数据。此统计信息用于为相对简单的对象树创建和显示GUI,其中包含大约500个对象。小部件总数介于1000-3000之间。创建GUI需要将近3秒钟。这太过分了。我想wxPython应该能够在一秒之内创建一千个小部件吗?
统计(前30名):
59475 function calls (59131 primitive calls) in 1.570 seconds
Ordered by: cumulative time
List reduced from 323 to 30 due to restriction <30>
ncalls tottime percall cumtime percall filename:lineno(function)
4/1 0.000 0.000 1.347 1.347 C:\Python\Projects\grafolizer\grafolizer\traits\layout.py:136(createEditor)
19/6 0.002 0.000 1.196 0.199 C:\Python\Projects\grafolizer\grafolizer\traits\layout.py:58(createEditor)
154 0.001 0.000 0.932 0.006 C:\Python\Projects\grafolizer\grafolizer\traits\value.py:212(createEditor)
154 0.001 0.000 0.918 0.006 C:\Python\Projects\grafolizer\grafolizer\traits\value.py:234(createRawEditor)
12 0.011 0.001 0.553 0.046 C:\Python\Projects\grafolizer\grafolizer\traits\layout.py:259(createEditor)
8 0.001 0.000 0.511 0.064 C:\Python\Projects\grafolizer\grafolizer\traits\editors.py:12(__init__)
49 0.000 0.000 0.499 0.010 C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py:1752(SetValue)
49 0.499 0.010 0.499 0.010 {wx._controls_.TextCtrl_SetValue}
8 0.000 0.000 0.495 0.062 C:\Python\Projects\grafolizer\grafolizer\traits\editors.py:51(DisplayValue)
131 0.011 0.000 0.288 0.002 C:\Python\Projects\grafolizer\grafolizer\traits\editors.py:430(__init__)
619 0.006 0.000 0.236 0.000 C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py:1130(__init__)
619 0.223 0.000 0.223 0.000 {wx._controls_.new_StaticText}
524 0.004 0.000 0.221 0.000 C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py:2564(__init__)
524 0.210 0.000 0.210 0.000 {wx._controls_.new_RadioButton}
13 0.000 0.000 0.107 0.008 C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py:8627(SetSize)
13 0.107 0.008 0.107 0.008 {wx._core_.Window_SetSize}
13 0.000 0.000 0.107 0.008 C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py:13057(Layout)
13 0.107 0.008 0.107 0.008 {wx._core_.Sizer_Layout}
12 0.001 0.000 0.083 0.007 C:\Python\Projects\grafolizer\grafolizer\traits\editors.py:60(__init__)
12 0.002 0.000 0.061 0.005 c:\Python\Lib\agw\floatspin.py:332(__init__)
2 0.000 0.000 0.051 0.026 C:\Python\Projects\grafolizer\grafolizer\traits\action.py:56(createEditor)
2 0.000 0.000 0.051 0.026 c:\Python\Lib\shopzeus\awx\buttons.py:38(__init__)
2 0.000 0.000 0.049 0.025 C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\lib\buttons.py:397(__init__)
2 0.000 0.000 0.046 0.023 C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\lib\buttons.py:431(SetBitmapLabel)
2 0.012 0.006 0.045 0.023 C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\lib\imageutils.py:15(grayOut)
13 0.000 0.000 0.039 0.003 C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py:9076(SetVirtualSize)
13 0.039 0.003 0.039 0.003 {wx._core_.Window_SetVirtualSize}
3 0.000 0.000 0.035 0.012 C:\Python\Projects\grafolizer\grafolizer\traits\editors.py:336(__init__)
12 0.000 0.000 0.034 0.003 C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_windows.py:177(SetScrollRate)
12 0.033 0.003 0.034 0.003 {wx._windows_.ScrolledWindow_SetScrollRate}
第136行的createEditor方法如下:
def createEditor(self,parent):
notebook = wx.Notebook(parent)
il = get_default_imagelist(*self.imgsize)
notebook.SetImageList(il)
for value in self.values:
widget = value[0].createEditor(notebook)
notebook.AddPage(widget,value[1], False)
if len(value)>2:
notebook.SetPageImage(notebook.GetPageCount()-1,il[value[2]])
return notebook
看起来原始函数调用占用总运行时间的大部分,即绑定wx事件。任何想法如何加快这一点?