Advertisement
Guest User

Untitled

a guest
May 1st, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. from collections import defaultdict
  2. from itertools import groupby
  3.  
  4. def lauritz(lst):
  5.     key = lambda dct: dct['id']
  6.     lst.sort(key=key)
  7.     ret = []
  8.     for key, group in groupby(lst, key=key):
  9.         ret.append(max(group, key=lambda dct: dct['s']))
  10.     return ret
  11.  
  12. def ashwini(lst):
  13.     dic={}
  14.     for x in lst:
  15.         idx=x['id']
  16.         if dic.get('s',float('-inf')) < x ['s']:
  17.             dic[idx]=x
  18.     return dic.values()
  19.  
  20. def martijn(lst):
  21.     per_id = defaultdict(list)
  22.  
  23.     for entry in lst:
  24.         per_id[entry['id']].append(entry)
  25.  
  26.     output = [max(v, key=lambda d: d['s']) for v in per_id.itervalues()]
  27.     return output
  28.  
  29. def thijs(lst):
  30.     seen = set()
  31.     output = [d for d in sorted(lst, key=lambda d: d['s'], reverse=True)
  32.           if d['id'] not in seen and not seen.add(d['id'])]
  33.     return output
  34.  
  35. def jamylak(lst):
  36.     res = {}
  37.     for d in lst:
  38.         id_ = d['id']
  39.         res[id_] = max(res.get(id_, {}), d, key=lambda x: x.get('s', float('-inf')))
  40.     return res.values()
  41.  
  42. import random
  43. import time
  44.  
  45. results = {}
  46. index = []
  47. for lst_len_pow in range(5, 19):
  48.     lst_len = 2 ** lst_len_pow
  49.     index.append(lst_len)
  50.     lst = [{
  51.         'id': random.randint(1, 10),
  52.         's': random.uniform(0, 5)} for x in range(lst_len)]
  53.     for func in (lauritz, ashwini, martijn, thijs, jamylak):
  54.         start = time.time()
  55.         func(lst)
  56.         results.setdefault(func.__name__, []).append(time.time() - start)
  57.  
  58. import pandas as pd
  59. import matplotlib.pyplot as plt
  60.  
  61. df = pd.DataFrame(results, index=index)
  62. df.plot()
  63. plt.loglog()
  64. plt.xlabel("list lenght")
  65. plt.ylabel("execution time")
  66. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement