Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 0.45 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. from __future__ import division
  3.  
  4. from random import random
  5. from timeit import timeit
  6.  
  7. def maxarg(arr):
  8.     counter = 0
  9.     arg = 0
  10.     m = arr[0]
  11.     for x in arr:
  12.         if x > m:
  13.             m = x
  14.             arg = counter
  15.         counter += 1
  16.     return arg
  17.  
  18. a = [random() for i in range(10000000)]
  19.  
  20. print timeit('a.index(max(a))', 'from __main__ import a', number=3)
  21. print timeit('maxarg(a)', 'from __main__ import a, maxarg', number=3)