Page 1 of 1

Slow Speed When Adding Items to ComboGadget

Posted: Wed Nov 05, 2014 4:56 pm
by swhite
Hi

Is there a way to add items to a comboGadget more quickly? I have to add 78 items to 8 comboGadgets and as a result the form displays incompletely until all the items are added to the comboGadgets.

Simon

Re: Slow Speed When Adding Items to ComboGadget

Posted: Wed Nov 05, 2014 5:14 pm
by ostapas
Does hiding the gadget while inserting new items help? Also, you could try this code (I think it's by NetMaestro)

Code: Select all

SendMessage_(GadgetID(your_gadget_id), #WM_SETREDRAW, 0, 0)
;insert items
SendMessage_(GadgetID(your_gadget_id), #WM_SETREDRAW, 1, 0)
InvalidateRect_(GadgetID(your_gadget_id), 0, 0)
UpdateWindow_(GadgetID(your_gadget_id))

Re: Slow Speed When Adding Items to ComboGadget

Posted: Wed Nov 05, 2014 9:28 pm
by swhite
It helps a little but there is still a noticeable delay before the window is updated. I also tried turning of the redraw for the window until it was complete which at least is better than seeing a partial complete window.

Re: Slow Speed When Adding Items to ComboGadget

Posted: Thu Nov 06, 2014 6:52 am
by Danilo
It would help if you could give a small example that shows the problem (so I don't have to do it for you ;)).
8 Comboboxes with 78 items each are displayed almost instantly here.

At program start, it is usually best to create an invisible window and display it
after creating and initializing/filling all gadgets.

Code: Select all

If OpenWindow(0, 0, 0, 270, 220, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible)
    For i = 0 To 7
        ComboBoxGadget(i, 10, 10+i*25, 250, 21)      ; create 8 ComboBoxGadget
    Next
    
    For i = 0 To 77
        For j = 0 To 7
            AddGadgetItem(j, -1, "ComboBox entry"+i) ; add new item
            SetGadgetState(j,CountGadgetItems(j)-1)  ; and activate new item
        Next
    Next
    
    HideWindow(0,#False)
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
By using #WM_SETREDRAW it is still almost instant here. ~60ms with 32-bit Compiler, ~40ms with 64-bit Compiler.

Code: Select all

If OpenWindow(0, 0, 0, 270, 220, "ComboBox Speed", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    For i = 0 To 7
        ComboBoxGadget(i, 10, 10+i*25, 250, 21)        ; create 8 ComboBoxGadget
    Next
    
    While WindowEvent():Wend                           ; flush event queue
    
    Delay(2000)                                        ; delay, just for testing
    
    time = ElapsedMilliseconds()
    
    For i = 0 To 7
        SendMessage_(GadgetID(i), #WM_SETREDRAW, 0, 0) ; disable gadget/window redrawing
    Next
    
    For i = 0 To 77
        For j = 0 To 7
            AddGadgetItem(j, -1, "ComboBox entry"+i)   ; add 78 * 8 = 624 new items
        Next
    Next
            
    For i = 0 To 7
        SetGadgetState(i,CountGadgetItems(i)-1)        ; activate last item
        SendMessage_(GadgetID(i), #WM_SETREDRAW, 1, 0) ; enable gadget/window redrawing
        InvalidateRect_(GadgetID(i), 0, 0)             ; invalidate control area
        UpdateWindow_(GadgetID(i))                     ; redraw invalidated area
    Next
    
    time = ElapsedMilliseconds()-time
    SetWindowTitle(0,"ComboBox Speed - "+Str(time)+" ms")
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Slow Speed When Adding Items to ComboGadget

Posted: Thu Nov 06, 2014 1:06 pm
by luis
Danilo wrote:It would help if you could give a small example that shows the problem (so I don't have to do it for you ;)).
And most often only to discover the actual code used was different because not enough details were given to guess it right anyway. :wink:

Solved: Slow Speed When Adding Items to ComboGadget

Posted: Thu Nov 06, 2014 3:27 pm
by swhite
I will remember to post code in the future but I found the problem thanks to Danilo's code. I did not set the #PB_Window_Invisible in the OpenWindow(). Once I did that the speed went back to what I expected. I had been looking for a redraw property or the like so I could set it to false before doing all the comboGadgets but did not realize that the #PB_Window_Invisible actually handled that for me.

It would be helpful if this were noted in the documentation because I did look for issues relating to redrawing etc. to see how it should be handled.

The moral of the story is set the #PB_Window_Invisible flag on the window before doing a lot of updating then use HideWindow(nWinNo,#False) to display the window afterward.

Thanks Danilo for your helpful example.

Re: Slow Speed When Adding Items to ComboGadget

Posted: Wed Jun 03, 2015 8:57 pm
by swhite
Hi

When I run this code on my computer using PB v5.31 32 or 64bit I get 1000-1200 ms. I am running Windows 8.1 on Intel iCore7 3.1 ghz with 16 gigabytes ram x64 processor (Dell XPS 27). This is alot slower than what you got.

Simon


Danilo wrote:It would help if you could give a small example that shows the problem (so I don't have to do it for you ;)).
8 Comboboxes with 78 items each are displayed almost instantly here.

At program start, it is usually best to create an invisible window and display it
after creating and initializing/filling all gadgets.

Code: Select all

If OpenWindow(0, 0, 0, 270, 220, "ComboBoxGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible)
    For i = 0 To 7
        ComboBoxGadget(i, 10, 10+i*25, 250, 21)      ; create 8 ComboBoxGadget
    Next
    
    For i = 0 To 77
        For j = 0 To 7
            AddGadgetItem(j, -1, "ComboBox entry"+i) ; add new item
            SetGadgetState(j,CountGadgetItems(j)-1)  ; and activate new item
        Next
    Next
    
    HideWindow(0,#False)
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
By using #WM_SETREDRAW it is still almost instant here. ~60ms with 32-bit Compiler, ~40ms with 64-bit Compiler.

Code: Select all

If OpenWindow(0, 0, 0, 270, 220, "ComboBox Speed", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    For i = 0 To 7
        ComboBoxGadget(i, 10, 10+i*25, 250, 21)        ; create 8 ComboBoxGadget
    Next
    
    While WindowEvent():Wend                           ; flush event queue
    
    Delay(2000)                                        ; delay, just for testing
    
    time = ElapsedMilliseconds()
    
    For i = 0 To 7
        SendMessage_(GadgetID(i), #WM_SETREDRAW, 0, 0) ; disable gadget/window redrawing
    Next
    
    For i = 0 To 77
        For j = 0 To 7
            AddGadgetItem(j, -1, "ComboBox entry"+i)   ; add 78 * 8 = 624 new items
        Next
    Next
            
    For i = 0 To 7
        SetGadgetState(i,CountGadgetItems(i)-1)        ; activate last item
        SendMessage_(GadgetID(i), #WM_SETREDRAW, 1, 0) ; enable gadget/window redrawing
        InvalidateRect_(GadgetID(i), 0, 0)             ; invalidate control area
        UpdateWindow_(GadgetID(i))                     ; redraw invalidated area
    Next
    
    time = ElapsedMilliseconds()-time
    SetWindowTitle(0,"ComboBox Speed - "+Str(time)+" ms")
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Slow Speed When Adding Items to ComboGadget

Posted: Wed Jun 03, 2015 11:06 pm
by IdeasVacuum
....I'm seeing ~60ms on XP with an Intel Core 2 Duo, so I think it is safe to say it's not the hardware.
Is it possible that your Anti-Virus software slows the app execution down? That is not an unusual scenario.

Re: Slow Speed When Adding Items to ComboGadget

Posted: Thu Jun 04, 2015 6:47 am
by sancho2
Its near instantaneous on my XP as well, and in debug mode. I don't know why its slow for you.
This is just a tip:
Inside out your nested ifs and you can remove 100's of unnecessary calls to setgadgetstate

Code: Select all

;For i = 0 To 77
;        For j = 0 To 7
;            AddGadgetItem(j, -1, "ComboBox entry"+i) ; add new item
;            SetGadgetState(j,CountGadgetItems(j)-1)  ; and activate new item
;        Next
;    Next

for j = 0 to 7
   for i = 0 to 77
       addgadgetitem(...)
   next 
   setgadgetstate(j, i - 1)
next
Edit:
I just noticed the original post and code was from quite a while ago and you were testing danillos code.

I added danillo's timing without turning off the window or doing any of the api calls that appear in danillos code.
Your original code ~290ms.
And with the revamped for next loops I got ~160ms

Re: Slow Speed When Adding Items to ComboGadget

Posted: Thu Jun 04, 2015 4:44 pm
by swhite
Hi

I disabled Windows Defender and the time went down to 330ms so it is about 3 times faster but still not to the level I would like. However, now I know it has to do with new security features in Windows 8.1.

Thanks,
Simon

Re: Slow Speed When Adding Items to ComboGadget

Posted: Thu Jun 04, 2015 6:36 pm
by uwekel
I am so happy to get rid of Windows for years. On my Linux laptop with Intel® Core™ i3-3227U CPU @ 1.90GHz it runs in 130ms without the need to hide the window :P

Re: Slow Speed When Adding Items to ComboGadget

Posted: Sat Jan 01, 2022 8:26 pm
by Michael Vogel
Filling a combogadget with items can be done with acceptable speed (as shown in this thread) but I can't speed up creating combogadgets. It takes around 80-100ms here (PB5.73x64, Windows 8.1) for one single combogadget!

The code below needs around a second which means the user needs to wait a while before the window will be shown. Other (not so complex) gadgets need only around 10 to 20% of the time of an empty combogadget. Could there be a hack to speed up this process?

Code: Select all

OpenWindow(0, 0, 0, 270, 380, "ComboBoxGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)

Debug ElapsedMilliseconds()
For i=0 To 10
	ComboBoxGadget(i, 10, 10+30*i, 250, 21, #PB_ComboBox_Editable)
	;EditorGadget(i, 10, 10+30*i, 250, 21)
	;DateGadget(i, 10, 10+30*i, 250, 21)
	;TreeGadget(i, 10, 10+30*i, 250, 21)
Next i
Debug ElapsedMilliseconds()

HideWindow(0,0)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Slow Speed When Adding Items to ComboGadget

Posted: Sat Jan 01, 2022 9:20 pm
by STARGÅTE
Here it takes 62ms for all 11 gadgets (PB 6.0, Windows 10). Don't know what happens on your PC.

Re: Slow Speed When Adding Items to ComboGadget

Posted: Sat Jan 01, 2022 9:28 pm
by Michael Vogel
Good question - my notebook is quite slow in general, but it takes around 5 times more time for a combogadget than for nearly all other gadget types (except explorergadgets which are also slow but not as slow as the combogadget). :shock: