FreeBasic Games Directory Forum

FreeBASIC Game Development => Programming => Topic started by: Pyramidschem on January 13, 2013, 04:21:26 PM

Title: About Min(a,b) math functions - Formula vs Compare
Post by: Pyramidschem on January 13, 2013, 04:21:26 PM
Hello,

First thanks for grant me clearance to this forum. Sorry for I am not too much fluent in english even if I borrow some game the basic vocabulary, and it is also true that google provides a correct translator...

-Edit :
I wanted to share with you a test I made in order to choose between 2 min(a,b) implementation. The first using the formula :
Code: [Select]
`    Min = ( R1 + R2 - Abs( R1 - R2) ) / 2`And the second using the case comparison
Code: [Select]
`select case R1        case Is <= R2            Min = R1        case else            Min = R2        End select `
I writed this test for that purpose :
Code: [Select]
`ScreenRes 600, 240, 16CONST AS integer Max_counter = 50000CONST AS integer Max_number = 1CONST AS integer Max_DurationArray = 500Declare Sub Test_Min_formula()Declare Sub Test_Min_CaseCompare()Declare Function Sum_Mean( Arr() as integer) as doubleDim shared AS double Timer_Start , Timer_End, DurationDim shared AS Integer _    DurationCaseCompare(Max_DurationArray),_    DurationFormula(Max_DurationArray)Dim shared AS integer aaa_counter, bbb_counter'***For bbb_counter = 0 to Max_DurationArray        Randomize Timer , 1        '---Test for Case compare method        Timer_Start = Timer        For aaa_counter = 1 to Max_counter            Test_Min_CaseCompare()            Next aaa_counter        Timer_End = Timer        Duration = Timer_End - Timer_Start                DurationCaseCompare(bbb_counter)=Duration*100000        Locate 1,1 : ? "Duration for case compare = ";_        DurationCaseCompare(bbb_counter)        '---Test for Direct Formula method        Timer_Start = Timer        For aaa_counter = 1 to Max_counter            Test_Min_formula()            Next aaa_counter        Timer_End = Timer        Duration = Timer_End - Timer_Start                DurationFormula(bbb_counter)=Duration*100000        Locate 5,1 : ? "Duration for formula = ";_        DurationFormula(bbb_counter)Next bbb_counter'RESULTSLocate 10,1 : ? "CaseCompare_mean = ";_Sum_Mean(DurationCaseCompare())Locate 11,1 : ? "Formula_mean = ";_Sum_Mean(DurationFormula())Locate 18,1 : ? "Factor diff. = ";_Sum_Mean(DurationFormula())/Sum_Mean(DurationCaseCompare())While Inkey <> "": Wend Sleep_ . _ . _ . _ . _ . _ . _ . _ . _ . Sub Test_Min_formula()Dim R1 As doubleDim R2 As doubleDim Min As double    R1 = 100 * Rnd    R2 = 100 * Rnd    Min = ( R1 + R2 - Abs( R1 - R2) ) / 2    End SubSub Test_Min_CaseCompare()Dim R1 As doubleDim R2 As doubleDim Min As double      R1 = 100 * Rnd    R2 = 100 * Rnd    select case R1        case Is <= R2            Min = R1        case else            Min = R2        End select    End SubFunction Sum_Mean(Arr() as integer) as doubleDim AS double sum = 0For bbb_counter = 0 to Max_DurationArray    sum = Arr(bbb_counter) + sumNext bbb_counterSum_Mean = sum / (Max_DurationArray+1)End function`
RESULT :

I found that the comparison using SELECT CASE was slightly faster. I have the duration of the min computation take 1.03 more time than the SelectCase usage.

Do you agree with that?