October 2025
Intermediate to advanced
485 pages
13h 2m
English
Out of curiosity, let’s benchmark Python’s built-in sort() method for arrays. Here’s my benchmarking code for timing the sorting of one million random integers:
| | import timeit |
| | |
| | setup_code = ''' |
| | import random |
| | |
| | array = [] |
| | for i in range(1_000_000): |
| | n = random.randint(1, 1_000_000) |
| | array.append(n) |
| | ''' |
| | |
| | |
| | test_code = ''' |
| | array.sort() |
| | ''' |
| | |
| | print(timeit.repeat(stmt=test_code, setup=setup_code, repeat=5, number=1)) |
Here are my results:
| | [0.7919449806213379, 0.7905678749084473, 1.1442229747772217, |
| | 1.0725150108337402, 1.0439801216125488] |
Pondering the meaning of this as I climb back into my chair, it appears that it almost always pays to use the sorting algorithm built ...
Read now
Unlock full access