Get shortest/longest string in array

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

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

@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.

1 Like

@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?

Are you using sizeof or length?

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")
2 Likes

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