# Get shortest/longest string in array

**URL:** <https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 21, 2021, 6:26pm UTC](https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739 "2021-01-21T18:26:37Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 22, 2021, 1:14pm UTC](https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739/21 "2021-01-22T13:14:28Z")

</div>

The benchmarks should be done using a larger array. For example using:

```julia
using Random
arr = [randstring(rand(1:256)) for _ in 1:10_000]

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 22, 2021, 1:24pm UTC](https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739/22 "2021-01-22T13:24:09Z")

</div>

> [@rafael.guerra](#):
>
> The benchmarks should be done using a larger array.

`@btime` can time short functions accurately (it calls them many times in a loop), and I did try a longer array. (Your longer array gives similar timings for me — my method is about 10% faster on Julia 1.6.) Note that once the array becomes sufficiently long, memory bandwidth will start to dominate.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 22, 2021, 1:34pm UTC](https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739/23 "2021-01-22T13:34:13Z")

</div>

@stevengj, I am sorry if I am doing something wrong but testing for the 10\_000 element Vector String above, on a regular Win10 laptop and Julia 1.6.0-beta1.0, @Seif_Shebl’s program btime is 13 μs (0 allocations: 0 bytes) while yours exhibits a btime of 598 μs (0 allocations: 0 bytes). Is this correct?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 22, 2021, 1:38pm UTC](https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739/24 "2021-01-22T13:38:23Z")

</div>

Are you using `sizeof` or `length`?

```julia
julia> str = [randstring(rand(1:256)) for _ in 1:10_000];

julia> @btime minmax_elements($str)
  13.399 μs (0 allocations: 0 bytes)
("P", "oZWjcJkz9IXRHCYHGsgJcXMt4owqtX4brzdymlxMT7e2dSP5C44wHCxTEMwP9hTecu5o0p9Q2Uk2w6zLLo0O60l1Em4QOAWSuzybNtM48kRwiYsofodqrzKgGH9mh0lCU3nU1GaHlryUunTk0w2qYQq5AxtGF9gvNDkVDBB3mCdUDb4r2sNAcmNN9j6950jKavZUycNczxTygpxJdWF6sRIfWQzrXWF4XHeDBkRRxNr5SWoEMBp0xaDQ48XZNdzU")

julia> @btime extrema_elements(length, $str)
  722.801 μs (0 allocations: 0 bytes)
("P", "oZWjcJkz9IXRHCYHGsgJcXMt4owqtX4brzdymlxMT7e2dSP5C44wHCxTEMwP9hTecu5o0p9Q2Uk2w6zLLo0O60l1Em4QOAWSuzybNtM48kRwiYsofodqrzKgGH9mh0lCU3nU1GaHlryUunTk0w2qYQq5AxtGF9gvNDkVDBB3mCdUDb4r2sNAcmNN9j6950jKavZUycNczxTygpxJdWF6sRIfWQzrXWF4XHeDBkRRxNr5SWoEMBp0xaDQ48XZNdzU")

julia> @btime extrema_elements(sizeof, $str)
  13.800 μs (0 allocations: 0 bytes)
("P", "oZWjcJkz9IXRHCYHGsgJcXMt4owqtX4brzdymlxMT7e2dSP5C44wHCxTEMwP9hTecu5o0p9Q2Uk2w6zLLo0O60l1Em4QOAWSuzybNtM48kRwiYsofodqrzKgGH9mh0lCU3nU1GaHlryUunTk0w2qYQq5AxtGF9gvNDkVDBB3mCdUDb4r2sNAcmNN9j6950jKavZUycNczxTygpxJdWF6sRIfWQzrXWF4XHeDBkRRxNr5SWoEMBp0xaDQ48XZNdzU")

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 22, 2021, 1:44pm UTC](https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739/25 "2021-01-22T13:44:12Z")

</div>

@DNF, I overlooked this, indeed. My apologies to @stevengj. And thank you both!

[Previous page](https://discourse.julialang.org/t/get-shortest-longest-string-in-array/53739.md?page=1)
