Slow StartDrawing

Everything else that doesn't fall into one of the other PB categories.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Slow StartDrawing

Post by Polo »

Hello,

Here's a code to point out something rather annoying:

Code: Select all

If OpenWindow(0, 0, 0, 460, 400, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  canvas = CanvasGadget(#PB_Any  , 10, 10, 380, 380, #PB_Canvas_ClipMouse)
  
  t=ElapsedMilliseconds()
  
  For i = 0 To 1000
    StartDrawing(CanvasOutput(canvas))
    StopDrawing()
  Next
  
  t2 = ElapsedMilliseconds() - t
  MessageRequester("",Str(t2))
  
  Repeat
    Event = WaitWindowEvent()
    
    
  Until Event = #PB_Event_CloseWindow

EndIf
It doesn't do anything of course, however the start/end drawing takes 1500 milliseconds on my Mac. If i swap to ImageOutput with an image, it then takes 300 milliseconds.

It makes no sense to do 1000 Start/EndDrawing here, but my code is using several Start/endrawing to get the text width and lenght, and is very slow when parsing 1000 lines because of what I shown before.

Why is it taking 1.5 seconds to do nothing? Why is it slower than when using an image?
User avatar
Demivec
Addict
Addict
Posts: 4283
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Slow StartDrawing

Post by Demivec »

Polo wrote:It doesn't do anything of course, however the start/end drawing takes 1500 milliseconds on my Mac. If i swap to ImageOutput with an image, it then takes 300 milliseconds.
It is my understanding that the CanvasGadget is double buffered. The ImageGagdet isn't double-buffered and so it makes sense that it would be faster.
Polo wrote:It makes no sense to do 1000 Start/EndDrawing here, but my code is using several Start/endrawing to get the text width and lenght, and is very slow when parsing 1000 lines because of what I shown before.
Whenever possible it is wise to combine operations that require Start/EndDrawing together so that any unnecessary delays are prevented.

Here's a link that may prove useful.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Slow StartDrawing

Post by Danilo »

Code: Select all

If OpenWindow(0, 0, 0, 460, 400, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  canvas = CanvasGadget(#PB_Any  , 10, 10, 380, 380, #PB_Canvas_ClipMouse)
 
  Dim sizes(1000)

  t=ElapsedMilliseconds()
 
  If StartDrawing(CanvasOutput(canvas))
      For i = 0 To 1000
        sizes(i) = TextWidth(Space(i)+"abcdfefhhhkhkh")
      Next
      StopDrawing()
  EndIf

  t2 = ElapsedMilliseconds() - t
  MessageRequester("",Str(t2))
 
  Repeat
    Event = WaitWindowEvent()
   
   
  Until Event = #PB_Event_CloseWindow

EndIf
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: Slow StartDrawing

Post by Polo »

Demivec wrote:
Polo wrote:It doesn't do anything of course, however the start/end drawing takes 1500 milliseconds on my Mac. If i swap to ImageOutput with an image, it then takes 300 milliseconds.
It is my understanding that the CanvasGadget is double buffered. The ImageGagdet isn't double-buffered and so it makes sense that it would be faster.
Drawing to an image doesn't use the ImageGadget :)
Can't use your code Danilo, if I'm calling StartDrawing many times it means it can't really be avoided! Although using a 1x1 image did the trick, my code is much faster now.
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Slow StartDrawing

Post by Perkin »

@Polo, - don't know whether it would be faster or not - can you just create an array of sizes for each individual character, which you fill in an initialising procedure, then when you need the width of a specific string, simply loop through each character and add the saved widths together?

You then shouldn't need all the StartDrawing/StopDrawing

EDIT: you should be able to speed this up as well.

Code: Select all

If OpenWindow(0, 0, 0, 460, 400, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  canvas = CanvasGadget(#PB_Any  , 10, 10, 380, 380, #PB_Canvas_ClipMouse)
  
  Dim SWid.l(255)
  If StartDrawing(CanvasOutput(canvas))
    For i = 32 To 127
      SWid(i) = TextWidth(Chr(i))
    Next
    StopDrawing()
  EndIf
  
  t=ElapsedMilliseconds()
  
  For i = 0 To 1000
    dum.s=Space(i)+"abcdfefhhhkhkh"
    res.l=0
    For j=0 To Len(dum)-1
      res+SWid(PeekC(@dum+j))
    Next
  Next
  
  t2 = ElapsedMilliseconds() - t
  MessageRequester("",Str(t2))
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
  
EndIf
%101010 = $2A = 42
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Slow StartDrawing

Post by Perkin »

As long as the text you need the width of is ASCII chars 32-127 then this is miles quicker,
change the timed loop to this

Code: Select all

  For i = 0 To 1000
    dum.s=Space(i)+"abcdfefhhhkhkh"
    res.l=0
    For j=32 To 127
      res+(CountString(dum,Chr(j))*SWid(j))
    Next
  Next
%101010 = $2A = 42
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: Slow StartDrawing

Post by Polo »

Well I'm using several fonts, with several sizes, and several attributes. Not sure I wanna store the data for everything....!
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Slow StartDrawing

Post by Perkin »

Ah well now, that makes a difference.
%101010 = $2A = 42
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: Slow StartDrawing

Post by Polo »

Plus Textwidth("abcdef") will not necessarily returns the same as Textwidth("a")+Textwidth("b") especially with an Italic font, due to a Purebasic bug.
Anyway, using an ImageOutput is quite fast so I'm using that for now :)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Slow StartDrawing

Post by Trond »

I think the canvas output is slow because on every StopDrawing() it tries to update the display. I'm just guessing here.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: Slow StartDrawing

Post by Polo »

Trond wrote:I think the canvas output is slow because on every StopDrawing() it tries to update the display. I'm just guessing here.
You're probably right, it'd explain everything.
User avatar
idle
Always Here
Always Here
Posts: 6238
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Slow StartDrawing

Post by idle »

I think it's because it's preserving the alpha channel in it's drawing routine
which slows it down, you'd be better off doing all your internal drawing into an image
and then draw that to the canvas when it's done.
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply