Page 1 of 1

Resize canvas slow on large monitor

Posted: Tue Jun 04, 2024 7:47 am
by coco2
Why is resizing canvases so slow in Windows? Below code shows a window with canvas on either side. If you run it on a large monitor with a high resolution like 4K the resizing is really slow. Anyone know why it's so slow?

https://justpaste.it/571z1

Re: Resize canvas slow on large monitor

Posted: Tue Jun 04, 2024 8:31 am
by Fred
A 4k canvas is a lot of pixels so it can become slow if you resize it.

Re: Resize canvas slow on large monitor

Posted: Tue Jun 04, 2024 10:24 am
by BarryG
Code like this can be slow:

Code: Select all

For c = 0 To numGadgets-1
Because PureBasic re-calculates "numGadgets-1" with every single loop iteration. I didn't look too deep at your code, but you should do that calculation just ONCE to a variable before the loop starts. That way you won't be wasting CPU cycles doing a calculation over and over.

Code: Select all

ng = numGadgets-1
For c = 0 To ng

Re: Resize canvas slow on large monitor

Posted: Tue Jun 04, 2024 11:00 am
by coco2
Thanks for the tip BarryG.

I have created the same interface using a canvas with drawn on canvases (rather that real canvases) and it is quite a bit faster on the 4K screen.

Would it be normal to create the whole interface using 2D primitives instead of the gadgets?

Re: Resize canvas slow on large monitor

Posted: Tue Jun 04, 2024 1:20 pm
by AZJIO
Give a simplified example with 4 gadgets. The fact that you call 4 nested functions and there repeatedly request the window size and call a bunch of other functions does not mean that the PureBasic engine is slow.

a lot of functions
WindowProcessEvents() -> #PB_Event_RestoreWindow -> WindowRestore() -> AutoResizeMax() -> MaxWidth()/ResizeGadget2() -> ResizeGadget()/Width2() -> WindowWidth()/x2()
BarryG wrote: Tue Jun 04, 2024 10:24 am

Code: Select all

numGadgets-1
This is nothing compared to what happens next...

Re: Resize canvas slow on large monitor

Posted: Wed Jun 05, 2024 12:46 am
by coco2
You might be right, I made this test code for resizing a canvas:

Code: Select all

OpenWindow(0, 0, 0, 400, 400, "Test", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_Maximize)
Define event.i
Define timeStart.i
CanvasGadget(0, 0, 0, 200, WindowHeight(0))
startTime = ElapsedMilliseconds()
ResizeGadget(0, 0, 0, WindowWidth(0), WindowHeight(0))
Debug "Resize time: " + Str(ElapsedMilliseconds() - startTime)
Repeat
  event = WaitWindowEvent(10)
Until event = #PB_Event_CloseWindow
I get 26 milliseconds for my 4K monitor which is about 38 FPS. I am definitely not getting 38 FPS in my application.

So actually Fred it is better than you thought :)

What do you think is causing the most slowness in my application?

Re: Resize canvas slow on large monitor

Posted: Wed Jun 05, 2024 1:45 am
by coco2
AZJIO, which aspect of nested functions is slowing it down? That is how an application works isn't it, with nested functions?

Re: Resize canvas slow on large monitor

Posted: Wed Jun 05, 2024 2:29 am
by Comfort

Code: Select all

Procedure GetEvents(Array events.EventStructure(1), *numEvents.Integer)
  Protected event.i
  *numEvents\i = 0
  event = WindowEvent()
  While event And *numEvents\i < #MAX_EVENTS
    events(*numEvents\i)\event = event
    events(*numEvents\i)\eventGadget = EventGadget()
    events(*numEvents\i)\eventType = EventType()
    *numEvents\i = *numEvents\i + 1
    event = WindowEvent()
  Wend
EndProcedure
I would look here.. collecting all events is not a great idea. Most are not needed.

Re: Resize canvas slow on large monitor

Posted: Wed Jun 05, 2024 2:33 am
by coco2
Thanks, I started collecting all events because a library I was using was clearing the events which was causing problems and I didn't know how to fix that without collecting them all. I am running tests on the timing of my application and should have some info soon.

Re: Resize canvas slow on large monitor

Posted: Wed Jun 05, 2024 3:03 am
by coco2
Comfort you are a genius, that was the bottleneck.

Thanks everyone for looking at my code and your assistance.