10.03.2019 - Round 1 results of our "A Love Letter For FreeBASIC" game dev competition have been published. Please be sure to check the results thread: http://games.freebasic.net/forum/index.php?topic=629.0. Don't forget that the competition is continuing with a round 2, lasting till 29th of April, 300 USD first prize. Stay tuned!

Author Topic: About Min(a,b) math functions - Formula vs Compare  (Read 1452 times)

Pyramidschem

  • Amoeba
  • *
  • Posts: 3
    • View Profile
About Min(a,b) math functions - Formula vs Compare
« 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, 16

CONST AS integer Max_counter = 50000
CONST AS integer Max_number = 1
CONST AS integer Max_DurationArray = 500

Declare Sub Test_Min_formula()
Declare Sub Test_Min_CaseCompare()
Declare Function Sum_Mean( Arr() as integer) as double

Dim shared AS double Timer_Start , Timer_End, Duration
Dim 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

'RESULTS
Locate 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 double
Dim R2 As double
Dim Min As double

    R1 = 100 * Rnd
    R2 = 100 * Rnd
    Min = ( R1 + R2 - Abs( R1 - R2) ) / 2
    
End Sub

Sub Test_Min_CaseCompare()
Dim R1 As double
Dim R2 As double
Dim Min As double
  
    R1 = 100 * Rnd
    R2 = 100 * Rnd
    select case R1
        case Is <= R2
            Min = R1
        case else
            Min = R2
        End select
    
End Sub

Function Sum_Mean(Arr() as integer) as double
Dim AS double sum = 0

For bbb_counter = 0 to Max_DurationArray
    sum = Arr(bbb_counter) + sum
Next bbb_counter

Sum_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?
« Last Edit: January 15, 2013, 08:57:14 AM by Pyramidschem »