How to shave off those few milliseconds...

Show Index

If your program is running sluggish, then here are some killer tips to improve the efficiency of your code.
Copy and paste the example code snippets in the right column to see what kind of a difference these tips make on your machine.

Note that the examples given are not necessarilly real world examples.  The examples are condensed down to the bare minimum code necessary in order to be easily understandable and some examples use what would usually be unrealisticly large loops just in order to show performance differences.  But they are all valid optimizations and when used together can get you a little more processing power out of your programs.
Also, the speed improvements listed (i.e. 30% faster) are approximations and vary from computer to computer.
These examples were run on a 2.5GHz processor and depending on your computers speed may be extremely slow...

 

The BEST improvements

Need to squeeze a little more?

STILL NEED MORE?

 

The BEST improvements:

Don't forget to turn off DEBUG
~2-70x faster than Debug mode

Debug mode is just for that, debugging.  For your final release version of your program, make sure you turn it off.
Try the code snippet on the right with/without Debug mode on.

start = MilliSecs()
For n = 0 To 100000000
	a = a + 1
Next
Print MilliSecs() - start

WaitKey
Don't use the Text command

Unfortunately, although the Text command is so easy to use, it's not very well optimized.  In fact, on some graphics cards, it will cause extreme slow-down, especially when used with 3D graphics.

If writing a 2D program, you're better off using bitmap fonts to display text onto the screen.  In a 3D program, you will do much better if you use/write a 3D text displaying function using quads(quads are single-sided two polygon meshes in the form of a rectangle).

Copy and paste the sample code on the right and tweak the values in the CreateSphere() commands to slow down your computer to about 200-300 fps.  Once you've got the fps set, remove the semicolons on the three Text lines in order to see what kind of difference using the Text command can make on your computer.

Graphics3D 800, 600
Type FrameRate
  Field FPS#[4]
  Field CurrentTicks
  Field FrameDelay
End Type
Global FL.FrameRate = New FrameRate
font = LoadFont("Tahoma", 200)
SetFont font
CreateLight()
CreateCamera()
CreateMirror()
;modify the sphere inputs on the following two lines
;to slow down your computer to a fps of about 200-300
;use numbers between 8-128 depending on your computers speed
sphere = CreateSphere(8)
sphere2 = CreateSphere(128)
MoveEntity sphere, -2, 2, 5
MoveEntity sphere2, 2, 2, 5
While Not KeyHit(1)
	TurnEntity sphere, .01, .2, .3
	TurnEntity sphere2, .01, .2, .3
	RenderWorld()
	;Text 10, 10, "Inefficient"
	;Text 10, 200, "Text"
	;Text 10, 400, "command"
	Flip False
	FL\CurrentTicks = MilliSecs()
	FL\FPS[0]=FL\FPS[1]+FL\FPS[2]+FL\FPS[3]+FL\FPS[4]
	FL\FPS[0]=(FL\FPS[0]+(1000.0/(FL\CurrentTicks-FL\FrameDelay)))/5.0
	FL\FPS[4]=FL\FPS[3]:FL\FPS[3]=FL\FPS[2]
	FL\FPS[2]=FL\FPS[1]:FL\FPS[1]=FL\FPS[0]
	FL\FrameDelay = FL\CurrentTicks
Wend
EndGraphics
Print "Frames per second:" + FL\FPS[0]
Print "Press a key to quit"
WaitKey
End
Don't use 2D drawing commands within a 3D program

Some graphics cards and/or video drivers are not well optimized to mix 2D drawing commands with 3D rendering modes.

Although it's very simple to add a background image to a 3D scene by using a 2D drawing command (DrawImage, DrawBlock, TileBlock, Rect, etc.) before calling RenderWorld() (and using a CameraCLSMode of zero), it may cause a very undesirable speed hit.

Graphics3D 640,480 
Text 0,0,"Use cursor keys to move about" 
Text 0,20,"Press space bar to enable/disable 2D background" 
Text 0,40,"Press Esc to end"
WaitKey()
camera=CreateCamera() 
PositionEntity camera,0,1,-4 
light=CreateLight() 
RotateEntity light,90,0,0 
plane=CreatePlane() 
ground_tex=CreateTexture(128, 128)
SetBuffer(TextureBuffer(ground_tex))
Color 50, 200, 20
Rect 0, 0, 128, 128
EntityTexture plane,ground_tex 
cube=CreateCube() 
PositionEntity cube,0,1,0 
background=CreateImage(128, 128)
SetBuffer(ImageBuffer(background))
Color 50, 50, 200
Rect 0, 0, 128, 128
SetBuffer BackBuffer()
Color 255, 0, 0
While Not KeyDown(1)
  If KeyHit(57)=True
    cls_color=1-cls_color 
    CameraClsMode camera,cls_color,1
  EndIf
  TileBlock background,0,0 
  RenderWorld 
  frames = frames + 1
  If MilliSecs() > OneSec
    fps = frames
    frames = 0
    OneSec = MilliSecs() + 1000
  EndIf
  Text 0, 0, fps
  Flip 
Wend
End

 

If you've reached this page and there's no index on the left, Click here to show the Index Page