GTK3应用程序中合理的小部件数量是多少?

时间:2016-12-04 15:21:38

标签: performance gtk3

我们有一个带有大量小部件3-4k的Python GTK应用程序。

当从GTK2移植到GTK3时,我们发现在以下情况下会有相当大的性能影响:

  1. 添加小部件。
  2. 只显示(不创建)窗口。
  3. 我的问题是:这是一个错误,还是在GTK3应用程序中使用数千个小部件是不合理的?

    以下测试计划perf.py显示了两个问题:

    import sys
    import gi
    import time
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    
    
    class DialogExample(Gtk.Dialog):
    
        def __init__(self, parent):
            Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                 Gtk.STOCK_OK, Gtk.ResponseType.OK))
    
            label = Gtk.Label("The snappiness of this dialog depends on the number of \nwidgets in the main program.")
            self.get_content_area().add(label)
            self.show_all()
    
    
    class DialogWindow(Gtk.Window):
    
        def __init__(self, count):
            Gtk.Window.__init__(self, title="Dialog Example")
    
            box = Gtk.Box()
            self.add(box)
    
            button = Gtk.Button("Open dialog")
            button.connect("clicked", self.on_button_clicked)
            box.pack_start(button, expand=False, fill=True, padding=0)
    
            # Add dummy widgets to show effect
            for n in xrange(count):
                button = Gtk.Button(str(n))
                button.set_no_show_all(True)
                t = time.time()
                box.pack_start(button, expand=False, fill=True, padding=0)
                print('{}\t{:.3f}'.format(n, 1000*(time.time()-t)))
    
        def on_button_clicked(self, widget):
            dialog = DialogExample(self)
            dialog.run()
            dialog.destroy()
    
    
    count = int(sys.argv[1])
    win = DialogWindow(count)
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()
    
    1. 为每个小部件添加小部件的时间会增加。
    2. 显示对话框所需的时间受应用程序中总小组件数的影响。
    3. Python脚本采用命令行参数,该参数是要创建的(隐藏)小部件的数量,并打印现有的小部件计数和添加它的时间。

      $ python perf.py 20000
      0       0.026
      1       0.021
      ...
      20000   1.700
      

      在20000个小部件中,即使在快速硬件上,第1点也非常清晰。第2点可能需要较慢的硬件或两倍的小部件数量才能清晰可见。

      下图显示了从大约20微秒到6毫秒的小部件创建时间。 Widget creation time (ms) vs number of widgets 排名第一的只是出乎意料,但第二点对我来说完全出乎意料。

      我在三台不同的机器上用Gtk 3.18测试了这个。

      主要应用程序窗口的一部分: Main application

0 个答案:

没有答案