Slow Speed When Adding Items to ComboGadget

Just starting out? Need help? Post your questions and find answers here.
swhite
Enthusiast
Enthusiast
Posts: 790
Joined: Thu May 21, 2009 6:56 pm

Slow Speed When Adding Items to ComboGadget

Post 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
Simon White
dCipher Computing
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Re: Slow Speed When Adding Items to ComboGadget

Post 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))
swhite
Enthusiast
Enthusiast
Posts: 790
Joined: Thu May 21, 2009 6:56 pm

Re: Slow Speed When Adding Items to ComboGadget

Post 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.
Simon White
dCipher Computing
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Slow Speed When Adding Items to ComboGadget

Post 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
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Slow Speed When Adding Items to ComboGadget

Post 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:
"Have you tried turning it off and on again ?"
A little PureBasic review
swhite
Enthusiast
Enthusiast
Posts: 790
Joined: Thu May 21, 2009 6:56 pm

Solved: Slow Speed When Adding Items to ComboGadget

Post 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.
Simon White
dCipher Computing
swhite
Enthusiast
Enthusiast
Posts: 790
Joined: Thu May 21, 2009 6:56 pm

Re: Slow Speed When Adding Items to ComboGadget

Post 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
Simon White
dCipher Computing
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Slow Speed When Adding Items to ComboGadget

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
sancho2
User
User
Posts: 44
Joined: Wed Apr 15, 2015 5:14 am

Re: Slow Speed When Adding Items to ComboGadget

Post 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
swhite
Enthusiast
Enthusiast
Posts: 790
Joined: Thu May 21, 2009 6:56 pm

Re: Slow Speed When Adding Items to ComboGadget

Post 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
Simon White
dCipher Computing
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: Slow Speed When Adding Items to ComboGadget

Post 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
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Slow Speed When Adding Items to ComboGadget

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Slow Speed When Adding Items to ComboGadget

Post by STARGÅTE »

Here it takes 62ms for all 11 gadgets (PB 6.0, Windows 10). Don't know what happens on your PC.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Slow Speed When Adding Items to ComboGadget

Post 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:
Post Reply