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.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)
Simpliest method compare numbers [Resolved]
- NicTheQuick
- Addict

- Posts: 1558
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: Simpliest method compare numbers [Resolved]
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.
- NicTheQuick
- Addict

- Posts: 1558
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: Simpliest method compare numbers [Resolved]
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.skywalk wrote: Tue Jan 13, 2026 2:30 amFor many of course, custom quicksort compare function for numeric array.
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.
Re: Simpliest method compare numbers [Resolved]
But it's branchless, so this is the most cool for me.NicTheQuick wrote: Tue Jan 13, 2026 10:32 amThat'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.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)
Re: Simpliest method compare numbers [Resolved]
The question was for the SIMPLEST not the fastest method. 
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
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
Re: Simpliest method compare numbers [Resolved]
True;) Still I'm glad with this find 
Re: Simpliest method compare numbers [Resolved]
Oh, I didn't mean especially you.
But true, it's cool to see how many possibilities we have to achieve this.
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
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
Re: Simpliest method compare numbers [Resolved]
In my applications, I only use it for the minimum (or maximum) of two integer values and sometimes with simple calculations inside.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.
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 Procedure = 281
Min = 2 - time Array = 298
- NicTheQuick
- Addict

- Posts: 1558
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: Simpliest method compare numbers [Resolved]
You can not measure such things using the debugger. It's always wrong.ChrisR wrote: Tue Jan 13, 2026 11:50 amMin = 2 - time Macro = 55
Min = 2 - time Procedure = 281
Min = 2 - time Array = 298
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()You can increase `Count` even more, it always takes 0 milliseconds because of compiler optimizations.Min = 2 - time Macro = 0
Min = 2 - time Procedure = 0
Min = 2 - time Array = 1060
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.
- NicTheQuick
- Addict

- Posts: 1558
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: Simpliest method compare numbers [Resolved]
Without compiler optimizations I can see that:
With this code:Min = 0 - sum = 2000000000 - time Macro = 6412
Min = 0 - sum = 2000000000 - time Procedure = 3061
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.
Re: Simpliest method compare numbers [Resolved]
Oops, of course, gulpNicTheQuick wrote: Tue Jan 13, 2026 1:38 pm You can not measure such things using the debugger. It's always wrong.
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'
Re: Simpliest method compare numbers [Resolved]
Code: Select all
UseSQLiteDatabase()
OpenDatabase(0, ":memory:", "", "", #PB_Database_SQLite)
DatabaseQuery(0, "Select Min(10, 2, 3, 5)")
NextDatabaseRow(0)
Debug GetDatabaseLong(0, 0)Hygge
- Mindphazer
- Enthusiast

- Posts: 526
- Joined: Mon Sep 10, 2012 10:41 am
- Location: Savoie
Re: Simpliest method compare numbers [Resolved]
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...
...and unfortunately... Windows at work...
Re: Simpliest method compare numbers [Resolved]
Me too, you gave me a big smile 
Re: Simpliest method compare numbers [Resolved]
But be ready to attach the SQLite module (1 Mb).
Re: Simpliest method compare numbers [Resolved]
Just LOVE It