Oct-17-2018, 09:17 PM
(Oct-17-2018, 08:56 PM)LeSchakal Wrote: and the results are:
2.84397915685567
0.6598219036493851
2.251084051447339
Ok, but because you use
.index()
, it's slower the more results you want (I believe O(n) is what it'd be called). For example, if you change it to be 1) an unsorted starting list (sorted starting list is cheating kind of), and b) more than just the max(2), you'll see a huge difference. Here's an example (I moved to a global seq
so each has a fair starting point): if __name__ == '__main__': import random, timeit seq = [x for x in range(10000)] random.shuffle(seq) print(timeit.timeit('n_max(seq, 100)', setup='from __main__ import seq, n_max', number=1000)) print(timeit.timeit('my_max(seq, 100)', setup='from __main__ import seq, my_max', number=1000)) print(timeit.timeit('n_max_2(seq, 100)', setup='from __main__ import seq, n_max_2, itemgetter', number=1000))
Output:3.9099400770000003
10.747478319
3.6070847269999984