back to home

list maximum perf testing

so i was watching this video

i was like "no way a for loop can be that much slower than `max()`"
so i decided to do some simple performance testing based on the file in the vid
YES, I KNOW THIS VIDEO IS A JOKE, I'M JUST HYPERFIXATING ON IT FOR SOME TIME

so the video gives a 5 kb file which magically turns into a 12.7 kb file, so i'm using 12.7 kb as the size
each number is about 22, 25 characters long, but i'll just use 20 chars to be generous

now a character is a single byte, so there's \(\frac{12.7 \cdot 10^3}{20 \cdot 1}=635\) numbers

and i'm already like "no goddamn way his code is this slow"
but i decided to test it anyway with ipython and see for myself

```py In [1]: import random In [2]: SIZE = 635 In [3]: %%timeit ...: arr = [random.uniform(0, 200) for _ in range(SIZE)] ...: max_value = arr[0] ...: for i in range(1, len(arr)): ...: if arr[i] > max_value: ...: max_value = arr[i] ...: 138 µs ± 2.59 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each) ```

yeah, that's way faster than what's shown in the video
but the discord user "P Solver" advocates for using `max()`, so i wanted to see how much faster that actually was just for the sake of it

```py In [4]: %%timeit ...: arr = [random.uniform(0, 200) for _ in range(SIZE)] ...: max_value = max(arr) 119 µs ± 536 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each) ```

hm, only faster by a couple of microseconds
but that's including the array generation (to simulate parsing the array, probably bad but)
ok let's just try raw maximum calculation

```py In [5]: %%timeit ...: max_value = arr[0] ...: for i in range(1, len(arr)): ...: if arr[i] > max_value: ...: max_value = arr[i] ...: 25.5 µs ± 477 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each) In [6]: %timeit max_value = max(arr) 5.12 µs ± 11.4 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) ```

ok yeah lmao i stand corrected
`max()` is goddamn five times faster than the pitiful loop

hate to admit it, but p solver was right
oh well, life isn't always fair

and yeah that's the whole post, nothing else lmfao