Code runs slow with fbc 64 bit..

General FreeBASIC programming questions.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Code runs slow with fbc 64 bit..

Post by mrminecrafttnt »

The compiled code is working faster in fbc 32 bit. In fbc 64 bit is it very slow but why?
Tested with fbc 1.05.0

Code: Select all

screenres 1920,1080


#undef pset



sub pset(y as integer,x as integer,c as integer)
    
    static s as ubyte ptr
    static w as integer
    
    if w = 0 then
        screeninfo w,,c
        s = screenptr
    end if
    s[(x*w)+y]=c
end sub

dim as double cx

do
    cx+=1
    screenlock

    for x as integer = 1 to 1900
        for y as integer = 1 to 1000
            pset x,y,(x*y)+cx
        next
    next
    screenunlock
    sleep 1
loop until inkey <> ""


print "PRESS A KEY TO EXIT"
sleep


dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Code runs slow with fbc 64 bit..

Post by dodicat »

Using direct pixels is very slow with 64 bit fbc.
I had noticed this before.
However 64 bit fbc is faster with openGL.

Code: Select all


#Include Once "GL/glu.bi"
Screenres 500,300,,,2
Sub GL2dsetup
    dim as integer wx,wy
    screeninfo wx,wy
    glMatrixMode (GL_PROJECTION)
    glLoadIdentity ()
    glOrtho (0,wx, wy,0, -1, 1)
    glMatrixMode (GL_MODELVIEW)
    glDisable (GL_DEPTH_TEST)
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
    glEnable (GL_BLEND)
    glEnable (GL_LINE_SMOOTH)
    glclearcolor 0,0,.2,1
    glLineWidth(1)
End Sub
GL2dsetup

type colour
 as ubyte r,g,b,a   
end type

Function framecounter() As Integer
    var t2=timer
    Static As Double t3,frames,answer
    frames=frames+1
    If (t2-t3)>=1 Then
        t3=t2
        answer=frames
        frames=0
    End If
    Return answer
End Function

Sub digits(t As String,x As long,y As long,clr As colour,sz As long)
    glend
        x=x-2*sz
        dim as single s=any,c=any
        #define nsz sz\20
        #macro thickline(x1,y1,x2,y2)
        glColor4ub(clr.r,clr.g,clr.b,clr.a)
             s=(y1-y2)/10
             c=(x2-x1)/10
             glend: glbegin gl_quads
    glvertex2f(x1+(s*-nsz),(y1+(c*-nsz)))
    glvertex2f(x1+(s*nsz),(y1+(c*nsz)))
    glvertex2f(x2+(s*nsz),(y2+(c*nsz)))
    glvertex2f(x2+(s*-nsz),(y2+(c*-nsz)))
    glend
        #endmacro
        #macro display(_a,_b,_c,_d,_e,_f,_g)
        x=x+2*sz
        If _a=1 Then :thickline(x,y,(x+sz),y):End If
        If _b=1 Then :thickline((x+sz),y,(x+sz),(y+sz)):End If
        If _c=1 Then :thickline((x+sz),(y+sz),(x+sz),(y+2*sz)):End If 
        If _d=1 Then :thickline((x+sz),(y+2*sz),x,(y+2*sz)):End If
        If _e=1 Then :thickline(x,(y+2*sz),x,(y+sz)):End If
        If _f=1 Then :thickline(x,(y+sz),x,y):End If
        If _g=1 Then :thickline(x,(y+sz),(x+sz),(y+sz)):End If
        #endmacro
        For z As long=0 To Len(t)-1
            Select Case As Const t[z]
            Case 48 :display(1,1,1,1,1,1,0)             '"0"
            Case 49 :display(0,1,1,0,0,0,0)             '"1"
            Case 50 :display(1,1,0,1,1,0,1)             '"2"
            Case 51 :display(1,1,1,1,0,0,1)             '"3"
            Case 52 :display(0,1,1,0,0,1,1)             '"4"
            Case 53 :display(1,0,1,1,0,1,1)             '"5"
            Case 54 :display(1,0,1,1,1,1,1)             '"6"
            Case 55 :display(1,1,1,0,0,0,0)             '"7"
            Case 56 :display(1,1,1,1,1,1,1)             '"8"
            Case 57 :display(1,1,1,1,0,1,1)             '"9"
            Case 58                                     '":"    
            glpointsize sz/4
            glbegin gl_points
            glvertex3f ((x+2*sz),(y+sz/2),0)
            glvertex3f ((x+2*sz),(y+1.5*sz),0)
            glend
            x=x+sz 
            Case 45 :display(0,0,0,0,0,0,1)              '"-"                       
            Case 46                                      '"."
            glpointsize sz/4
            glbegin gl_points
            glvertex3f((x+2*sz),(y+1.9*sz),0)
            glend
            x=x+sz 
            Case 32                                      '" "
            x=x+sz 
            End Select
        Next z
        glend
    End Sub  
    
windowtitle "FRAMERATE"

do
    glClear (GL_COLOR_BUFFER_BIT)
digits(str(framecounter),100,100,type(200,100,0,255),25)
'sleep 1
    Flip
Loop Until inkey=Chr(27)
 
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Code runs slow with fbc 64 bit..

Post by paul doe »

mrminecrafttnt wrote:The compiled code is working faster in fbc 32 bit. In fbc 64 bit is it very slow but why?
Reeeeally?

Code: Select all

dim as integer screenWidth = 1280, screenHeight = 720
dim shared as integer scrPitch, scrBpp

screenRes( screenWidth, screenHeight, 8 )
screenInfo( , , , scrBpp, scrPitch )

#undef pset

sub pset( byval x as integer, byval y as integer, byval c as ubyte )
	static s as ubyte ptr
	
	if( s = 0 ) then
		s = screenPtr()
	end if
	
	s[ ( y * scrPitch ) + x ] = c
end sub

'dim as double cx
dim as integer cx
dim as double t, sum
dim as integer count

dim as ubyte ptr scr = screenPtr()
dim as ubyte ptr pix

do
	cx += 1
	
	screenlock()
		t = timer()
	  
	  pix = scr
	  
	  for y as integer = 0 to screenHeight - 1
	      for x as integer = 0 to ( scrPitch \ scrBpp ) - 1
	          'pset( x, y, ( x * y ) + cx )
	          *pix = ( x * y ) + cx
	          
	          pix += 1
	      next
	  next
	  
	  t = timer() - t
	
		sum += t
		count += 1
	screenUnlock()
	
	windowTitle( "FPS: " & int( 1 / ( sum / count ) ) )
	
	sleep( 1 )  
loop until( inkey() <> "" )
Compile it with fbc 64-bit (use -gen gcc -Wc -O3) and see if it's really slower (I get ~800 FPS in GCC 32 bit, ~600 FPS in 64-bit, halve that on GAS). So, you're doing several things wrong, if you want to directly plot a pixel to the pixel buffer with maximum speed:
  • You're iterating the screen in the reverse order in which you should (the 'natural' way is Y-X, instead of X-Y)
  • You're calculating the pixel position wrongly. See the code above for a correction in your pset() routine.
  • If you're going to use a function to plot the pixel, try to pass the parameters byval, as counter-intuitive as this advice is. This is because if you use the GCC backend (there's no other backend in 64-bit), it will inline the call as an optimization.
But the worst speed killer in your code, believe it or not, is this seemingly innocuous line:

Code: Select all

dim as double cx
You're declaring 'cx' as double, and then using it in an integer expression that calculates the pixel color. This has to do with how a compiler evaluates an expression that mixes data types. Declare it as integer (as in the code above) to avoid implicit conversions, as converting an int to/from float is not a cheap operation.
Last edited by paul doe on Dec 21, 2017 4:43, edited 2 times in total.
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Code runs slow with fbc 64 bit..

Post by paul doe »

dodicat wrote:However 64 bit fbc is faster with openGL.
Mmmm, I get a blue screen. This is supposed to show the framerate with FFP OpenGL primitives, no?
Post Reply