slow draw again

Just starting out? Need help? Post your questions and find answers here.
ludoke
Enthusiast
Enthusiast
Posts: 153
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

slow draw again

Post by ludoke »

I am still not happy with the speed to draw something.
In an earlier post I mentioned that before, but the problem was slow loading in a list gadget.
Loading an array with data from a file is much faster than a list gadget.
I have to process a text file of thousands of lines of graphic data.These data are just strings with XYZ coordinates.I want to zoom, move and rotate the data on the srceen.
Drawing only 500 rectangles already takes a long time.
I don't understand what I'm doing wrong, when I look at other graphics programs it all goes very quickly

Code: Select all

Enumeration  
  #main_win: #font
  #F1     ;functietoets met add
  #canvas
  #test_File:#list1
EndEnumeration  
;-------------------------------------------------------
Global win_color.l=RGB(115,122,121)
Global x1,y1  ;start line
Global X2,Y2  ;end line
Global line_color.l
Global starttime.q,endtime.q,time.q
Global Dim my_line.s(100)
Global Dim test.s(100001)
;-------------------------------------------------------
Declare sneltoets()
;-------------------------------------------------------
  LoadFont(#font,"caladea",18, #PB_Font_Italic|#PB_Font_Bold) 
  flags=#PB_Window_SystemMenu;| #PB_Window_Maximize;#PB_Window3D_Borderless;|#PB_Window_ScreenCentered 
 ;array with lines 
  my_line(1)="100,100,200,100"
  my_line(2)="200,100,200,200"
  my_line(3)="200,200,100,200"
  my_line(4)="100,200,100,100"
  ;-------------------------------------------------------
  Procedure make_test_file();make a file with 100000 lines of strings
    Define fo
    Define n.l
    fo = CreateFile(#test_File, "test_file.txt")
    For n=0 To 100000
      WriteStringN(#test_file,Str(n)+"x100,y100,z100") 
    Next
    EndProcedure 
 ;---------------------------------------------------
    Procedure test_array_speed();it takes 55 ms !!
      number_lines.l=0
      StartTime.q = ElapsedMilliseconds()  
       If OpenFile(  #test_file, "test_file.txt")  ; opens an existing file or creates one, if it does not exist yet
          ReadFile(  #test_file, "test_file.txt")   ; if the file could be read, we continue...
       While Eof( #test_file) = 0      ; loop as long the 'end of file' isn't reached      
         test(number_lines)=Str(number_lines)+"   "+ReadString(#test_file) 
          number_lines= number_lines+1
       ; Debug ReadString(#file)      ; display line by line in the debug window 
      Wend
      CloseFile( #test_file) 
    EndIf
      endtime.q= ElapsedMilliseconds() 
              time=endtime-StartTime
                Debug "time_array = "+time
                Debug "lines="+number_lines
  EndProcedure  
  ;------------------------------------------------------------  
  Procedure list_speed();it takes 11398 ms
    StartTime.q = ElapsedMilliseconds() 
    number_lines.l=0
     If OpenFile(  #test_file, "test_file.txt")  ; opens an existing file or creates one, if it does not exist yet
          ReadFile(  #test_file, "test_file.txt")   ; if the file could be read, we continue...
        While Eof( #test_file) = 0      ; loop as long the 'end of file' isn't reached      
            AddGadgetItem (#list1, -1,  Str(number_lines)+"   "+ReadString(#test_file)) ; define listview content
             number_lines= number_lines+1
     ; Debug ReadString(#file)      ; display line by line in the debug window 
        Wend
        CloseFile( #test_file);SetActiveGadget(#main_window)  
      EndIf
       endtime.q= ElapsedMilliseconds() 
            time=endtime-StartTime
              Debug "time_list = "+time
                Debug "lines="+number_lines
  EndProcedure  
  ;-----------------------------------------------------
  Procedure tekenlijn(a1,b1,a2,b2)
    StartDrawing(CanvasOutput(#canvas))
        LineXY(a1,b1,a2,b2,line_color)   
    StopDrawing()
  EndProcedure
  ;------------------------------------------------------------------------
  Procedure draw_box(mx,my) ;draw 500 box
    Define u,t,n,e
    Define l$
    For e=1 To 500  ;number of box
      t=Random(400) ;random y position
      u=Random(400) ;random x position
    For n=1 To 4
      l$=my_line(n)
      x1= ValD(StringField(l$,1,","))
      Y1= ValD(StringField(l$,2,","))
      x2= ValD(StringField(l$,3,","))
      Y2= ValD(StringField(l$,4,","))   
      tekenlijn(X1+mx+u,y1+my+t,x2+mx+u,y2+my+t)
    Next  
      Next      
   EndProcedure 
;----------------------------------------------------------
Procedure OnCanvasEvents()
  Protected mX, mY, y
  mX = GetGadgetAttribute(#canvas, #PB_Canvas_MouseX)
  mY = GetGadgetAttribute(#canvas, #PB_Canvas_MouseY)
  Select EventType()
    Case #PB_EventType_MouseMove, #PB_EventType_LeftClick
      If GetGadgetAttribute(#canvas, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton
         StartDrawing(CanvasOutput(#canvas))
           Box( 0,0,880,600,$F3F3F3) ;clear canvas
           line_color=$D1101F
         StopDrawing()        
             draw_box(mx,my)  
      EndIf
    
  EndSelect
EndProcedure
;-----------------------------------------------------------------------
If OpenWindow (#main_win,0,0,1200,900, "box test" ,flags )
  CanvasGadget(#canvas,10,10,880,600)
  ListViewGadget(#list1,900,200,200,400)
  make_test_file()
  list_speed() ;test the time for listview
  test_array_speed();test time for fill an array
  BindGadgetEvent(#canvas, @OnCanvasEvents(), #PB_All)
  sneltoets()   
Repeat
    Ev = WaitWindowEvent()
    Select ev   ;
            Case   #PB_Event_Gadget                
        Select EventGadget()               
          Case #F1     ;main
               ; tekenlijn(x1,y1,x2,y2)         
        EndSelect
      Case    #PB_Event_Menu 
        Select EventMenu()            
          Case #F1
            ;tekenlijn(x1,y1,x2,y2)
        EndSelect        
EndSelect
    Until ev=#PB_Event_CloseWindow 
EndIf
End   
;--------------------------------------------------------
Procedure sneltoets()  ;sneltoetsen voor keybord events
   AddKeyboardShortcut(#main_win,  #PB_Shortcut_F1,#F1)
 EndProcedure
 
User avatar
STARGÅTE
Addict
Addict
Posts: 2089
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: slow draw again

Post by STARGÅTE »

First, I'm shocked, when I see that you have a procedure tekenlijn(), where you start and stop the drawing each time.
This procedure is called 2000 times (why there is a second loop from n = 1 to 4 ???) in draw_box(mx,my).
StartDrawing():StopDrawing() is "verry slow", usually you call this just once and then draw all stuff in it.

Second, store all coordinates from your string in a structure to avoid to call StringField() each time.

Third, do not measure the time in debugger mode.
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
ludoke
Enthusiast
Enthusiast
Posts: 153
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

Re: slow draw again

Post by ludoke »

Stargate,
thanks,I'm going to come up with a different strategy.
For a beginner, coming up with the right strategy is not easy.
I did not expect start drawing, stop drawing to be slow.
I now only have lines, but there are also arcs and circles in the graphic files .
I'm going to try to put everything in a structure first ,and then in its entirety in StartDrawing (): StopDrawing ()
But I will always have to decode line by line, each line can contain different data.
Post Reply