Simpliest method compare numbers [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
NicTheQuick
Addict
Addict
Posts: 1558
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Simpliest method compare numbers [Resolved]

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
NicTheQuick
Addict
Addict
Posts: 1558
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Simpliest method compare numbers [Resolved]

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
miso
Enthusiast
Enthusiast
Posts: 662
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Simpliest method compare numbers [Resolved]

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 2062
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Simpliest method compare numbers [Resolved]

Post by jacdelad »

The question was for the SIMPLEST not the fastest method. :wink:
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
miso
Enthusiast
Enthusiast
Posts: 662
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Simpliest method compare numbers [Resolved]

Post by miso »

True;) Still I'm glad with this find :)
User avatar
jacdelad
Addict
Addict
Posts: 2062
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Simpliest method compare numbers [Resolved]

Post by jacdelad »

Oh, I didn't mean especially you.
But true, it's cool to see how many possibilities we have to achieve this.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
User avatar
ChrisR
Addict
Addict
Posts: 1544
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Simpliest method compare numbers [Resolved]

Post 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
User avatar
NicTheQuick
Addict
Addict
Posts: 1558
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Simpliest method compare numbers [Resolved]

Post 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.
Last edited by NicTheQuick on Tue Jan 13, 2026 1:41 pm, edited 1 time in total.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
NicTheQuick
Addict
Addict
Posts: 1558
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Simpliest method compare numbers [Resolved]

Post 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()
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
ChrisR
Addict
Addict
Posts: 1544
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Simpliest method compare numbers [Resolved]

Post 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:
User avatar
Kiffi
Addict
Addict
Posts: 1515
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Simpliest method compare numbers [Resolved]

Post 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
Hygge
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 526
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Simpliest method compare numbers [Resolved]

Post by Mindphazer »

I love it Kiffi !
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 26.1 - Iphone 17 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
ChrisR
Addict
Addict
Posts: 1544
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Simpliest method compare numbers [Resolved]

Post by ChrisR »

Me too, you gave me a big smile :lol:
AZJIO
Addict
Addict
Posts: 2254
Joined: Sun May 14, 2017 1:48 am

Re: Simpliest method compare numbers [Resolved]

Post 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).
User avatar
Piero
Addict
Addict
Posts: 1173
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Simpliest method compare numbers [Resolved]

Post 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:
Post Reply