# Obtain highest array value?

**URL:** <https://forum.xojo.com/t/obtain-highest-array-value/24927>\
**Category:** General\
**Created:** [June 14, 2015, 11:57pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927 "2015-06-14T23:57:13Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [June 15, 2015, 1:25pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/21 "2015-06-15T13:25:45Z")

</div>

I tested three techniques to get the highest integer in an array.

1. highest = Max( element, highest )
2. if element \> highest then highest = element
3. highest = if( element \> highest, element, highest )

Here are the results from a compiled app:

```auto
Max: 147,603 microsecs (1000000)
If: 8,950 microsecs (1000000)
Inline If: 9,225 microsecs (1000000)
```

It looks like my aversion to Max was justified. 🙂

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [June 15, 2015, 1:32pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/22 "2015-06-15T13:32:50Z")

</div>

And if you create your own Max function that takes and returns Integers, the “Max” version drops to 30,654 microsecs.

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [June 15, 2015, 1:50pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/23 "2015-06-15T13:50:52Z")

</div>

```auto
Dim highest as integer = myArray(0)
for each element as integer in myArray()
 highest=max(highest,element)
next
```

should be faster yet

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [June 15, 2015, 1:51pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/24 "2015-06-15T13:51:14Z")

</div>

> [@194410:@Kem Tekinay](#):
>
> And if you create your own Max function that takes and returns Integers, the “Max” version drops to 30,654 microsecs.

why create you own… one already exists

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [June 15, 2015, 1:54pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/25 "2015-06-15T13:54:46Z")

</div>

I think you missed my post with the timings above. Max is the slowest, probably (my speculation) because it takes and returns doubles so there is time to make the conversion. That’s why your own Max function that takes and returns Integers is faster.

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [June 15, 2015, 2:09pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/26 "2015-06-15T14:09:33Z")

</div>

never mind… I need to read the whole thread 🙂

---

<div class="post-metadata">

**Author:** ![Charles\_Yeomans](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/848f3c/32.png) [@Charles\_Yeomans](https://forum.xojo.com/u/Charles_Yeomans)\
**Post date:** [June 15, 2015, 4:35pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/27 "2015-06-15T16:35:24Z")

</div>

Here’s the fastest algorithm.

[code]Function Max(L() as Integer) As Integer  
if UBound(L) = -1 then  
raise new OutOfBoundsException  
end if

dim maxValue as Integer = L(0)  
for i as Integer = 1 to UBound(L)  
if L(i) \> maxValue then  
maxValue = L(i)  
end if  
next

return maxValue  
End Function[/code]

Sorting will always be slower, though on a small array the absolute time difference is negligible.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [June 15, 2015, 4:49pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/28 "2015-06-15T16:49:15Z")

</div>

You don’t need to check for the empty array. Attempting to assign the first value to maxValue will raise the same exception if the array is empty.

Also, I doubt this is faster than using For Each since you are trading the single, redundant check of the zero element for two lookups in the array of every matching value. You could assign the value to a variable, of course, but For Each is faster at that task. (I just tested.)

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [June 15, 2015, 5:13pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/29 "2015-06-15T17:13:01Z")

</div>

For Each is a bit faster:

[code] Dim arr() As Integer

For i As Integer = 0 To 1000000  
Dim r As New Random()  
r.RandomizeSeed()  
arr.Append(r.InRange(-999999, 999999))  
Next

Dim t1ForIndex As Double = Microseconds()

Dim largestForIndex As Integer = arr(0)

Dim ub As Integer = arr.Ubound  
For i As Integer = 0 To ub  
Dim element As Integer = arr(i)  
If element \> largestForIndex Then  
largestForIndex = element  
End  
Next

Dim t2ForIndex As Double = Microseconds()

Dim t1ForEach As Double = Microseconds()

Dim largestForEach As Integer = arr(0)

For Each element As Integer In arr  
If element \> largestForEach Then  
largestForEach = element  
End  
Next

Dim t2ForEach As Double = Microseconds()

Dim t1Sort As Double = Microseconds()

arr.Sort()  
Dim largestSort As Integer = arr(arr.Ubound)

Dim t2Sort As Double = Microseconds()

Dim durationForIndex As Double = (t2ForIndex - t1ForIndex) / 1000000  
Dim durationForEach As Double = (t2ForEach - t1ForEach) / 1000000  
Dim durationSort As Double = (t2Sort - t1Sort) / 1000000

BREAK[/code]

1. For Each
2. Sort
3. For index

---

<div class="post-metadata">

**Author:** ![Richard\_Summers](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_summers/32/477_2.png) [@Richard\_Summers](https://forum.xojo.com/u/Richard_Summers)\
**Post date:** [June 15, 2015, 5:17pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/30 "2015-06-15T17:17:17Z")

</div>

So have we all agreed which is the fastest - or are you guys still battling it out?  
🙂

---

<div class="post-metadata">

**Author:** ![Charles\_Yeomans](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/848f3c/32.png) [@Charles\_Yeomans](https://forum.xojo.com/u/Charles_Yeomans)\
**Post date:** [June 15, 2015, 5:44pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/31 "2015-06-15T17:44:15Z")

</div>

[quote=194448:@Kem Tekinay]You don’t need to check for the empty array. Attempting to assign the first value to maxValue will raise the same exception if the array is empty.

Also, I doubt this is faster than using For Each since you are trading the single, redundant check of the zero element for two lookups in the array of every matching value. You could assign the value to a variable, of course, but For Each is faster at that task. (I just tested.)[/quote]

I said that it was the fastest algorithm. Obviously there are minor optimizations specific to Xojo to be done.

Testing for an empty array makes it clear that the OutOfBoundsException is intended, and not the result of a programming error.

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [June 15, 2015, 5:46pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/32 "2015-06-15T17:46:33Z")

</div>

Update: For index is the faster than For Each and than Sort, but only when not assigning the array element to a temporary variable:

` Dim ub As Integer = arr.Ubound
  For i As Integer = 0 To ub
    If arr(i) > largestForIndex Then
      largestForIndex = arr(i)
    End
  Next`  
is faster than:

` Dim ub As Integer = arr.Ubound
  For i As Integer = 0 To ub
    Dim element As Integer = arr(i)
    If element > largestForIndex Then
      largestForIndex = element
    End
  Next`

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [June 15, 2015, 5:48pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/33 "2015-06-15T17:48:39Z")

</div>

Update 2: removing the precomputed arr.Ubound is faster!!!

` For i As Integer = 0 To arr.Ubound
    If arr(i) > largestForIndex Then
      largestForIndex = arr(i)
    End
  Next`  
is faster than:

` Dim ub As Integer = arr.Ubound
  For i As Integer = 0 To ub
    If arr(i) > largestForIndex Then
      largestForIndex = arr(i)
    End
  Next`

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [June 15, 2015, 6:11pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/34 "2015-06-15T18:11:23Z")

</div>

Are you testing in a compiled app or the ide?

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [June 15, 2015, 6:16pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/35 "2015-06-15T18:16:16Z")

</div>

Compiled.

Another update: Sort is the fastest solution for middle-sized arrays, for small ones (\< 1000) and large ones (\> 100000) For Index is still fastest.

But I am going to stop now, or I’ll have to invest the whole evening in proper benchmarking. I think I’ll better go out and have a beer or two.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [June 15, 2015, 6:17pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/36 "2015-06-15T18:17:09Z")

</div>

I think I just blew these timings away by using a memoryblock instead of an array

Try this:

[code]  
Dim arr() As Integer  
dim i as integer

For i = 0 To 1000000  
Dim r As New Random()  
r.RandomizeSeed()  
arr.Append(r.InRange(-999999, 999999))  
Next

//copy the same array to a memoryblock

dim m2 as new MemoryBlock (1000001 \* 4)  
for i = 0 to 1000000  
m2.uint32value(i) = arr(i)  
next

largestForIndex = m2.uint32value(0)  
Dim ub1 As Integer = 100000  
For i = 0 To ub1  
If m2.uint32value(i) \> largestForIndex Then  
largestForIndex = m2.uint32value(i)  
End  
Next[/code]

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [June 15, 2015, 6:26pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/37 "2015-06-15T18:26:08Z")

</div>

I thought about memory blocks too. But both the classic and the Xojo.Core one are slower, and this doesn’t include the time for copying the values to the memory block.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [June 15, 2015, 6:27pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/38 "2015-06-15T18:27:17Z")

</div>

> [@194493:@Eli Ott](#):
>
> I thought about memory blocks too. But both the classic and the Xojo.Core one are slower, and this doesn’t include the time for copying the values to the memory block.

I think we’re going off the deep end now. 🙂

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [June 15, 2015, 6:38pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/39 "2015-06-15T18:38:09Z")

</div>

> [@](#):
>
> I thought about memory blocks too. But both the classic and the Xojo.Core one are slower, and this doesn’t include the time for copying the values to the memory block.

Unless I timed it wrong I was seeing values of 0.08 for the baseline test, and 0.009 for using the memoryblock

I only copy from the array to the memblock so that they both had the same data.  
Obviously if your source data originates in an array, it would have to be copied.  
Could it originate in a memoryblock too?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [June 15, 2015, 10:37pm UTC](https://forum.xojo.com/t/obtain-highest-array-value/24927/40 "2015-06-15T22:37:36Z")

</div>

My tests show 9 ms to analyze an array where Ubound = 1,000,000. Is this optimization worth the inconvenience of delving into a MemoryBlock-based array? Or is this just for fun?

[Previous page](https://forum.xojo.com/t/obtain-highest-array-value/24927.md?page=1)

[Next page](https://forum.xojo.com/t/obtain-highest-array-value/24927.md?page=3)
