Skip to content

.sort_runs()

Jacob Morris edited this page Jun 7, 2019 · 3 revisions

.sort_runs()

Available via Timer.sort_runs() and provides a way to sort the runs in all splits or in a specific split. By default, all splits will be sorted unless split_index is set as a valid split name or index. By default, the runs are sorted by time, from least to greatest, but that direction can be flipped by setting reverse=True.

More complicated options are available for sorting. For example, logged arguments can be sorted on. This can be done by passing the index or key of the argument to keys. If you are trying to sort all the splits at once, then keys can also be a map of a split label/index to an index or key of the argument you want to sort on.

keys=1, keys="age" and keys={"binary_search": "array", 1: 0} are all valid. The first sorts on the position argument at index 1, the second sorts on the keyword argument age, and the last sorts the split labeled "binary_search" on a keyword argument named "array" and the split at index 1 on a positional argument at index 0.

Important: All values being sorted on must be comparable. If an argument is not, then it needs to be transformed into something that is. This can be done by passing a function to transformers that will take the value, pass it to the given function, and then sort on the result of that function call. transformers=len is valid, as is transformers={"binary_search": len, "adder": sum}.

timer = Timer()
timer.time_it(sorted, lambda: [randint(0, 100000) for _ in range(randint(0, 10000))], call_callable_args=True,
              log_arguments=True, runs=5)

timer.output(split_index="sorted", transformers={0: len})
# sorted:
#        0.01819 ms - sorted(97)                                 [runs=  1, iterations=  1]                     
#        1.84187 ms - sorted(9292)                               [runs=  1, iterations=  1]                     
#        1.20645 ms - sorted(6107)                               [runs=  1, iterations=  1]                     
#        1.33568 ms - sorted(6674)                               [runs=  1, iterations=  1]                     
#        0.88501 ms - sorted(5212)                               [runs=  1, iterations=  1]     

timer.sort_runs()
timer.output(split_index="sorted", transformers={0: len})
# sorted:
#        0.01819 ms - sorted(97)                                 [runs=  1, iterations=  1]                     
#        0.88501 ms - sorted(5212)                               [runs=  1, iterations=  1]                     
#        1.20645 ms - sorted(6107)                               [runs=  1, iterations=  1]                     
#        1.33568 ms - sorted(6674)                               [runs=  1, iterations=  1]                     
#        1.84187 ms - sorted(9292)                               [runs=  1, iterations=  1] 

timer.sort_runs(keys=0, transformers=len, reverse=True)
timer.output(split_index="sorted", transformers={0: len})     
# sorted:
#        1.84187 ms - sorted(9292)                               [runs=  1, iterations=  1]
#        1.33568 ms - sorted(6674)                               [runs=  1, iterations=  1]   
#        1.20645 ms - sorted(6107)                               [runs=  1, iterations=  1] 
#        0.88501 ms - sorted(5212)                               [runs=  1, iterations=  1] 
#        0.01819 ms - sorted(97)                                 [runs=  1, iterations=  1]                     

Clone this wiki locally