I need to write a Python generator that produces all possible pairs of numbers in range 0..N. The pairs must be sorted by a pair's sum. Is it possible to implement that CPU-efficiently?
Sequence example:
(0, 0), (0, 1), (1, 0), (0, 2), (1, 1), (2, 0), (1, 2), (2, 1), (2, 2)
A poor implementation, which takes 125ms for N=1000:
N = 1000
# t1 = time.time()
pairs = [(i, j) for i in range(N) for j in range(N)]
pairs2 = list(sorted(pairs, key=sum))
# t2 = time.time()
# print(f'took {t2 - t1} s, n={n}')
# print(pairs2)
A generator is preferred because there are many cases when the iteration will stop rather soon, so I'd expect ~zero time consumption then.