October 2025
Intermediate to advanced
485 pages
13h 2m
English
This is the fun part—benchmarking our sorting algorithms! I saved our Mergesort code from Code Implementation: Mergesort inside a file called mergesort_1.py. The strategy we’ll follow to benchmark our code is to first generate an array of integers in random order and then benchmark how quickly Mergesort can sort the array. (The following code contains some new elements we haven’t encountered yet, but I’ll walk through them shortly.)
| | import timeit |
| | |
| | setup_code = ''' |
| | import random |
| | import mergesort_1 |
| | |
| | array = [] |
| | for i in range(10): |
| | n = random.randint(1, 1000) |
| | array.append(n) |
| | ''' |
| | |
| | test_code = ''' |
| | mergesort_1.mergesort(array) |
| | ''' |
| | |
| | print(timeit.repeat(stmt=test_code, setup=setup_code, ... |
Read now
Unlock full access