quick poll - precalc sin & cos or perform every frame ?

BlitzPlus Forums/BlitzPlus Programming/quick poll - precalc sin & cos or perform every frame ?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

After comparing apples to apples (your code was doing an extra (unfair) conversion of floating point to integer):
Graphics 640,480,32,2

;Lookup Table
Dim Sin2#(360)
Dim Cos2#(360)
For a=0 To 359
	Sin2#(a)=Sin(a)
	Cos2#(a)=Cos(a)
Next

Iterations=71500   ; change

; Calc on the fly
Time1=MilliSecs()
For t=1 To Iterations
    For a=0 To 359
        dx# = Cos(a) 
        dy# = Sin(a)
    Next
Next
Time2=MilliSecs()
TimeTaken1=Time2-Time1


; Now Calc using tables
Time1=MilliSecs()
For t=1 To Iterations
    For a=0 To 359
        dx# = Cos2#(a) 
        dy# = Sin2#(a)
    Next
Next
Time2=MilliSecs()
TimeTaken2=Time2-Time1


Text 10,10,"REPORT"
Text 10,30,"Calc On the fly took "+Str(TimeTaken1)+" millisecs"
Text 10,45,"Look up tables took "+Str(TimeTaken2)+" millisecs"

Flip
WaitKey()
End
this is the results I came up with for
B+ :6693 - 95
B3D:5531 - 122 (approx. 4307 - 95)

Looks like B3D is faster at doing the math than B+ is (35% faster) and slower at looking up the tables.

My machine is a P4-2.53GHz.
I imagine with such small tables (360 elements), it fits entirely in the L2 cache so the processor doesn't have to access the system RAM. With a larger table this may not be true.

I got these results with substantially larger tables which suggest what I was talking about.
B+ :3102 - 103
B3D:2565 - 99
Graphics 640,480,32,2

;Lookup Table
Dim Sin2#(36000000)
Dim Cos2#(36000000)
For a=0 To 35999999
	Sin2#(a)=Sin(a)
	Cos2#(a)=Cos(a)
Next

Iterations=1   ; change

; Calc on the fly
Time1=MilliSecs()
For t=1 To Iterations
    For a=0 To 35999999
        dx# = Cos(a) 
        dy# = Sin(a)
    Next
Next
Time2=MilliSecs()
TimeTaken1=Time2-Time1


; Now Calc using tables
Time1=MilliSecs()
For t=1 To Iterations
    For a=0 To 35999999
        dx# = Cos2#(a) 
        dy# = Sin2#(a)
    Next
Next
Time2=MilliSecs()
TimeTaken2=Time2-Time1


Text 10,10,"REPORT"
Text 10,30,"Calc On the fly took "+Str(TimeTaken1)+" millisecs"
Text 10,45,"Look up tables took "+Str(TimeTaken2)+" millisecs"

Flip
WaitKey()
End