#!/usr/bin/env python3 from operator import itemgetter from timeit import default_timer as timer def single_pass_data(): return [ { 'col0': i // (8 ** 0) % 8, 'col1': i // (8 ** 1) % 8, 'col2': i // (8 ** 2) % 8, 'col3': i // (8 ** 3) % 8, 'col4': i // (8 ** 4) % 8, 'col5': i // (8 ** 5) % 8, 'col6': i // (8 ** 6) % 8, 'col7': i, } for i in range(8 ** 8) ] def multi_pass_data(): return [ { 'col0': i, 'col1': 0, 'col2': 0, 'col3': 0, 'col4': 0, 'col5': 0, 'col6': 0, 'col7': 0, } for i in range(8 ** 8) ] def single_pass_func(data): data.sort(key=lambda x: ( itemgetter('col7')(x), itemgetter('col6')(x), itemgetter('col5')(x), itemgetter('col4')(x), itemgetter('col3')(x), itemgetter('col2')(x), itemgetter('col1')(x), itemgetter('col0')(x), )) def multi_pass_func(data): data.sort(key=itemgetter('col0')) data.sort(key=itemgetter('col1')) data.sort(key=itemgetter('col2')) data.sort(key=itemgetter('col3')) data.sort(key=itemgetter('col4')) data.sort(key=itemgetter('col5')) data.sort(key=itemgetter('col6')) data.sort(key=itemgetter('col7')) if __name__ == '__main__': for data in (single_pass_data, multi_pass_data): _data = data() for func in (single_pass_func, multi_pass_func): t = timer() func(_data) print('{}({})={}'.format(func.__name__, data.__name__, timer() - t))