Page 2 of 2

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 10:32 am
by NicTheQuick
ChrisR wrote: Mon Jan 12, 2026 7:07 pm My macros, pretty close to SMaag's one
Macros are probably the fastest method, but beyond 4 variables, an array may be better

Code: Select all

Macro Min(a, b)
  (Bool(a < b) * a + Bool(a < b)!1 * b)
EndMacro

Macro Max(a, b)
  (Bool(a > b) * a + Bool(a > b)!1 * b)
EndMacro

a=10
b=3
c=2
d=5

Debug Min(Min(Min(a, b), c), d)
Debug Max(Max(Max(a, b), c), d)
That's not right. There are way to many operators involved and these macros are very inefficient if `a` or `b` are more complex terms an not just constants like in the example.

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 10:37 am
by NicTheQuick
skywalk wrote: Tue Jan 13, 2026 2:30 amFor many of course, custom quicksort compare function for numeric array.
Quicksort (= O(n·log(n))) is slower than just iterating over the array (= O(n)) and picking the lowest value. Quicksort is only helpful if you want to retrieve the lowest number first, and then the second lowest and so on.

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 10:38 am
by miso
NicTheQuick wrote: Tue Jan 13, 2026 10:32 am
ChrisR wrote: Mon Jan 12, 2026 7:07 pm My macros, pretty close to SMaag's one
Macros are probably the fastest method, but beyond 4 variables, an array may be better

Code: Select all

Macro Min(a, b)
  (Bool(a < b) * a + Bool(a < b)!1 * b)
EndMacro

Macro Max(a, b)
  (Bool(a > b) * a + Bool(a > b)!1 * b)
EndMacro

a=10
b=3
c=2
d=5

Debug Min(Min(Min(a, b), c), d)
Debug Max(Max(Max(a, b), c), d)
That's not right. There are way to many operators involved and these macros are very inefficient if `a` or `b` are more complex terms an not just constants like in the example.
But it's branchless, so this is the most cool for me.

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 11:04 am
by jacdelad
The question was for the SIMPLEST not the fastest method. :wink:

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 11:06 am
by miso
True;) Still I'm glad with this find :)

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 11:16 am
by jacdelad
Oh, I didn't mean especially you.
But true, it's cool to see how many possibilities we have to achieve this.

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 11:50 am
by ChrisR
NicTheQuick wrote: Tue Jan 13, 2026 10:32 am That's not right. There are way to many operators involved and these macros are very inefficient if `a` or `b` are more complex terms an not just constants like in the example.
In my applications, I only use it for the minimum (or maximum) of two integer values and sometimes with simple calculations inside.
With simple conditions, it is the fastest and also the easiest method, just a call to the macro already prepared

Code: Select all

Macro Min(a, b)
  (Bool(a < b) * a + Bool(a < b)!1 * b)
EndMacro

Macro Max(a, b)
  (Bool(a > b) * a + Bool(a > b)!1 * b)
EndMacro

Procedure min4(a, b, c, d)
  Protected min = a
  If b < min: min = b: EndIf
  If c < min: min = c: EndIf
  If d < min: ProcedureReturn d: EndIf
  ProcedureReturn min
EndProcedure

a=15 : b=2 : c=10 : d=3
Count = 1000000

startTime = ElapsedMilliseconds()
For i = 1 To Count
  Value = Min(Min(a, b), Min(c, d))
Next
Debug "Min = " + Value + " - time Macro = " + Str(ElapsedMilliseconds() - startTime) 

startTime = ElapsedMilliseconds()
For i = 1 To Count
  Value = min4(a, b, c, d)
Next
Debug "Min = " + Value + " - time Procedure = " + Str(ElapsedMilliseconds() - startTime) 

Dim tmparray(3)
tmparray(0)=a : tmparray(1)=b : tmparray(2)=c : tmparray(3)=d
startTime = ElapsedMilliseconds()
For i = 1 To Count
  Value = tmparray(0)
  For j = 1 To 3  ; ArraySize(tmparray())
    If Value > tmparray(j)
      Value = tmparray(j)
    EndIf
  Next
Next
Debug "Min = " + Value + " - time Array = " + Str(ElapsedMilliseconds() - startTime)
Min = 2 - time Macro = 55
Min = 2 - time Procedure = 281
Min = 2 - time Array = 298

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 1:38 pm
by NicTheQuick
ChrisR wrote: Tue Jan 13, 2026 11:50 amMin = 2 - time Macro = 55
Min = 2 - time Procedure = 281
Min = 2 - time Array = 298
You can not measure such things using the debugger. It's always wrong.
Also compiler optimizations change the result since the C compiler recognizes that the `value` is not being used except after the last iteration.

Therefore I changed your code to this:

Code: Select all

Macro Min(a, b)
  (Bool(a < b) * a + Bool(a < b)!1 * b)
EndMacro

Macro Max(a, b)
  (Bool(a > b) * a + Bool(a > b)!1 * b)
EndMacro

Procedure min4(a, b, c, d)
  Protected min = a
  If b < min: min = b: EndIf
  If c < min: min = c: EndIf
  If d < min: ProcedureReturn d: EndIf
  ProcedureReturn min
EndProcedure

a=15 : b=2 : c=10 : d=3
Count = 10000000000

OpenConsole()

startTime = ElapsedMilliseconds()
For i = 1 To Count
  Value = Min(Min(a, b), Min(c, d))
Next
PrintN("Min = " + Value + " - time Macro = " + Str(ElapsedMilliseconds() - startTime))

startTime = ElapsedMilliseconds()
For i = 1 To Count
  Value = min4(a, b, c, d)
Next
PrintN("Min = " + Value + " - time Procedure = " + Str(ElapsedMilliseconds() - startTime))

Dim tmparray(3)
tmparray(0)=a : tmparray(1)=b : tmparray(2)=c : tmparray(3)=d
startTime = ElapsedMilliseconds()
For i = 1 To Count
  Value = tmparray(0)
  For j = 1 To 3  ; ArraySize(tmparray())
    If Value > tmparray(j)
      Value = tmparray(j)
    EndIf
  Next
Next
PrintN("Min = " + Value + " - time Array = " + Str(ElapsedMilliseconds() - startTime))

Input()
CloseConsole()
And the result with `Count = 10000000000` is:
Min = 2 - time Macro = 0
Min = 2 - time Procedure = 0
Min = 2 - time Array = 1060
You can increase `Count` even more, it always takes 0 milliseconds because of compiler optimizations.

I also tried to sum up all the results into a variable `sum` but even then the compiler understood the code and that all the values are static. So the time always was 0 ms, even with `Count = 10.000.000.000.000` (10 trillion).

The conclusion might be, that we're both correct. Both codes are fast when using compiler optimizations.

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 1:40 pm
by NicTheQuick
Without compiler optimizations I can see that:
Min = 0 - sum = 2000000000 - time Macro = 6412
Min = 0 - sum = 2000000000 - time Procedure = 3061
With this code:

Code: Select all

a=15 : b=2 : c=10 : d=3
Count = 1000000000

OpenConsole()

startTime = ElapsedMilliseconds()
sum=0
For i = 1 To Count
  sum + Min(Min(a, b), Min(c, d))
Next
PrintN("Min = " + Value + " - sum = " + sum + " - time Macro = " + Str(ElapsedMilliseconds() - startTime))

startTime = ElapsedMilliseconds()
sum = 0
For i = 1 To Count
  sum + min4(a, b, c, d)
Next
PrintN("Min = " + Value + " - sum = " + sum + " - time Procedure = " + Str(ElapsedMilliseconds() - startTime))

Input()
CloseConsole()

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 2:55 pm
by ChrisR
NicTheQuick wrote: Tue Jan 13, 2026 1:38 pm You can not measure such things using the debugger. It's always wrong.
Oops, of course, gulp :oops:
So, I'm fine with your conclusion.
With compiler optimizations and 0ms even with `Count = 10.000.000.000.000`, it's almost 'Back to the Future' :wink:

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 4:08 pm
by Kiffi
jacdelad wrote: Tue Jan 13, 2026 11:04 am The question was for the SIMPLEST not the fastest method. :wink:

Code: Select all

UseSQLiteDatabase()
OpenDatabase(0, ":memory:", "", "", #PB_Database_SQLite)
DatabaseQuery(0, "Select Min(10, 2, 3, 5)")
NextDatabaseRow(0)
Debug GetDatabaseLong(0, 0)
:D

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 5:13 pm
by Mindphazer
I love it Kiffi !

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 5:25 pm
by ChrisR
Me too, you gave me a big smile :lol:

Re: Simpliest method compare numbers [Resolved]

Posted: Tue Jan 13, 2026 6:45 pm
by AZJIO
Mindphazer wrote: Tue Jan 13, 2026 5:13 pm I love it Kiffi !
But be ready to attach the SQLite module (1 Mb).

Re: Simpliest method compare numbers [Resolved]

Posted: Thu Jan 15, 2026 3:55 pm
by Piero
jacdelad wrote: Tue Jan 13, 2026 11:04 am The question was for the SIMPLEST not the fastest method. :wink:
Just LOVE It :P :lol: