import timeit
import __main__
from itertools import izip, count
from operator import itemgetter
import random
N = 100000
L = [1,2,3,4,1,2]
def test_f_FJ_1():
-min((x, -i) for i, x in enumerate(L))[1]
def test_f_FJ_2():
min(range(len(L)), key=lambda i: (L[i], -i))
def test_f_FJ_2_modified():
min(range(len(L)), key=lambda i: L[i]-i)
def test_f_Michel_Keijzers():
L.reverse()
len(L) - L.index(min(L)) - 1
def test_f_user2387370():
len(L) - L[::-1].index(min(L)) - 1
def test_f_akson128():
min_val = min(L)
for i in range(len(L)):
if L[i] == min_val:
idx = i
def test_f_Stephan():
len(L)-1 - L[::-1].index(min(L))
def test_f_iCodez():
max(x for x,y in enumerate(L) if y == min(L))
def test_f_ovgolovin():
min(izip(count(len(L)-1,-1), reversed(L)), key=itemgetter(1))[0]
def main():
names = dir(__main__)
for name in names:
attr = getattr(__main__, name)
if hasattr(attr,'__call__'):
if name.startswith('test_f_'):
init_name = name + '_init'
if hasattr(__main__, init_name):
init = getattr(__main__, init_name)
else:
init = 'from __main__ import {name}'.format(name=name)
t = timeit.Timer(stmt='{name}()'.format(name=name),
setup=init)
time = t.timeit(N)
print('{name}: {time}'.format(name=name,time=time))
print("Small list")
print("=========================")
main()
L = range(10000)
#random.shuffle(L)
N = 100
print
print("Large list")
print("=========================")
del test_f_iCodez # too slow because of quadratic complexity
main()
aW1wb3J0IHRpbWVpdAppbXBvcnQgX19tYWluX18KZnJvbSBpdGVydG9vbHMgaW1wb3J0IGl6aXAsIGNvdW50CmZyb20gb3BlcmF0b3IgaW1wb3J0IGl0ZW1nZXR0ZXIKaW1wb3J0IHJhbmRvbQoKTiA9IDEwMDAwMApMID0gWzEsMiwzLDQsMSwyXQoKCmRlZiB0ZXN0X2ZfRkpfMSgpOgogICAgLW1pbigoeCwgLWkpIGZvciBpLCB4IGluIGVudW1lcmF0ZShMKSlbMV0KICAgIApkZWYgdGVzdF9mX0ZKXzIoKToKICAgIG1pbihyYW5nZShsZW4oTCkpLCBrZXk9bGFtYmRhIGk6I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FzYXR0cihfX21haW5fXywgaW5pdF9uYW1lKToKICAgICAgICAgICAgICAgICAgICBpbml0ID0gZ2V0YXR0cihfX21haW5fXywgaW5pdF9uYW1lKQogICAgICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgICAgICBpbml0ID0gJ2Zyb20gX19tYWluX18gaW1wb3J0IHtuYW1lfScuZm9ybWF0KG5hbWU9bmFtZSkKICAgICAgICAgICAgICAgIHQgPSB0aW1laXQuVGltZXIoc3RtdD0ne25hbWV9KCknLmZvcm1hdChuYW1lPW5hbWUpLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzZXR1cD1pbml0KQogICAgCiAgICAgICAgICAgICAgICB0aW1lID0gdC50aW1laXQoTikKICAgICAgICAgICAgICAgIHByaW50KCd7bmFtZX06IHt0aW1lfScuZm9ybWF0KG5hbWU9bmFtZSx0aW1lPXRpbWUpKQogICAgICAgICAgICAgICAgCgpwcmludCgiU21hbGwgbGlzdCIpCnByaW50KCI9PT09PT09PT09PT09PT09PT09PT09PT09IikKbWFpbigpCgoKTCA9IHJhbmdlKDEwMDAwKQojcmFuZG9tLnNodWZmbGUoTCkKTiA9IDEwMApwcmludApwcmludCgiTGFyZ2UgbGlzdCIpCnByaW50KCI9PT09PT09PT09PT09PT09PT09PT09PT09IikKZGVsIHRlc3RfZl9pQ29kZXogIyB0b28gc2xvdyBiZWNhdXNlIG9mIHF1YWRyYXRpYyBjb21wbGV4aXR5Cm1haW4oKQ==