• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
SHARE
TWEET # argmax.py a guest Jun 15th, 2011 546 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import time
2. import random
3.
4. def maxarg(arr):
5.     counter = 0
6.     arg = 0
7.     m = arr
8.     for x in arr:
9.         if x > m:
10.             m = x
11.             arg = counter
12.         counter += 1
13.     return arg
14.
15. if __name__=="__main__":
16.   arrays=[[i for i in range(1000000)] for j in range(10)]
17.   for x in arrays: random.shuffle(x)
18.   before = time.time()
19.   for array in arrays: print ("    ignore this :" , array.index(max(array)))
20.   after = time.time()
21.   print( after-before)
22.   before = time.time()
23.   for array in arrays: print ("    ignore this :" , maxarg(array))
24.   after = time.time()
25.   print( after-before)
26.   before = time.time()
27.   for array in arrays: print ("    ignore this :" , max(zip(array, range(len(array)))))
28.   after = time.time()
29.   print( after-before)
30.   before = time.time()
31.   for array in arrays: print("    ignore this :" ,  max([array[i],i] for i in range(len(array))))
32.   after = time.time()
33.   print (after-before)
34.   before = time.time()
35.   for array in arrays: print("    ignore this :" ,  max((array[i],i) for i in range(len(array))))
36.   after = time.time()
37.   print (after-before)
38.   before = time.time()
39.   for array in arrays:  print("    ignore this :" , max(range(len(array)), key=array.__getitem__))
40.   after = time.time()
41.   print (after-before)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!

Top