4 ms·
Now try with a for loop.
by otteromkram 2y ago
Now try with a for loop.
- memco 2y agoFor science I added versions of the benchmarks as for loops: it's faster than the while loop versions above, but min(heights) is still way faster: def benchmark1for(heights): smallest = heights[0] for h in heights: if h < smallest: smallest = h return smallest def benchmark2for(heights): smallest = heights[0] for h in heights: smallest = min(h, smallest) return smallest Results: min(heights) : 0.07920974999433383 benchmark1 : 0.954568124958314 benchmark1for: 0.5765543330344371 benchmark2 : 1.8662503749947064 benchmark2for: 1.5281645830255002
- necovek 2y agoGood call GP, and thanks for the results! What was the Python version you used, and what was the CPU? My runs were on 3.11 on Ryzen 7 Pro 6850U with whatever the TDP envelope is on a Thinkpad T14s.
- memco 2y agoPython 3.12.1 on an M2 macbook air. For extra fun I also tested doing `sorted(heights)[0]` which is slightly faster than the benchmark2 variants, but slower than the benchmark1 variants.