Resize canvas slow on large monitor

Just starting out? Need help? Post your questions and find answers here.
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Resize canvas slow on large monitor

Post 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
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Resize canvas slow on large monitor

Post by Fred »

A 4k canvas is a lot of pixels so it can become slow if you resize it.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Resize canvas slow on large monitor

Post 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
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Resize canvas slow on large monitor

Post 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?
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Resize canvas slow on large monitor

Post 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...
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Resize canvas slow on large monitor

Post 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?
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Resize canvas slow on large monitor

Post by coco2 »

AZJIO, which aspect of nested functions is slowing it down? That is how an application works isn't it, with nested functions?
Comfort
User
User
Posts: 32
Joined: Thu Jul 05, 2018 11:52 pm

Re: Resize canvas slow on large monitor

Post 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.
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Resize canvas slow on large monitor

Post 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.
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Resize canvas slow on large monitor

Post by coco2 »

Comfort you are a genius, that was the bottleneck.

Thanks everyone for looking at my code and your assistance.
Post Reply