fork download
  1. # your code goes here
  2. from timeit import timeit
  3.  
  4. print(timeit(stmt='''
  5. us, cs = np.unique(a, return_counts = True)
  6. ui = np.searchsorted(us, a)
  7. af = cs[ui]
  8. if desc:
  9. ix = np.argsort(-af, kind = 'stable') # Descending sort
  10. else:
  11. ix = np.argsort(af, kind = 'stable') # Ascending sort
  12. ''', setup='''
  13. import numpy as np
  14. a = np.array(['g', 'g', 'c', 'f', 'd', 'd', 'g', 'a', 'a', 'a', 'f', 'f', 'f',
  15. 'g', 'f', 'c', 'f', 'a', 'e', 'b', 'g', 'd', 'c', 'b', 'f' ])
  16. desc=True
  17. ''', number=1000))
  18.  
  19. print(timeit(stmt='''
  20. counts = Counter(a)
  21. t = [(counts[v], i) for i, v in enumerate(a)]
  22. t.sort(key = lambda x:(-x[0],x[1]))
  23. ''', setup='''
  24. from collections import Counter
  25. a = ['g', 'g', 'c', 'f', 'd', 'd', 'g', 'a', 'a', 'a', 'f', 'f', 'f',
  26. 'g', 'f', 'c', 'f', 'a', 'e', 'b', 'g', 'd', 'c', 'b', 'f' ]
  27. ''', number=1000))
Success #stdin #stdout 0.23s 27564KB
stdin
Standard input is empty
stdout
0.03054763376712799
0.011215902864933014