mengzhuo
2013-08-22 11:37:00 +08:00
[t11.append(t) for t in l if t not in t11]
1000000 loops, best of 3: 793 ns per loop
# 不过话说这么个复杂度是2n吧
[dict(t) for t in set([tuple(d.items()) for d in l])]
100000 loops, best of 3: 9.25 us per loop
[{k:v} for k,v in dict([list(d.iteritems())[0] for d in t1]).iteritems()]
100000 loops, best of 3: 10.4 us per loop
[{k:v} for k,v in dict([d.items()[0] for d in t1]).items()]
100000 loops, best of 3: 5.01 us per loop
# 这个很漂亮
---------------------------
升级一下测试:
t2 = [{random.randint(0, 4095):1} for k in xrange(9999)]
len([d for d in t2 if t2.count(d) > 1]) # 这次随机出9144项重复的
[t22.append(t) for t in t2 if t not in t22]
1 loops, best of 3: 1.54 s per loop
# 直接跪了 = =
[dict(t) for t in set([tuple(d.items()) for d in t2])]
10 loops, best of 3: 21.9 ms per loop
[{k:v} for k,v in dict([list(d.iteritems())[0] for d in t2]).iteritems()]
10 loops, best of 3: 26 ms per loop
[{k:v} for k,v in dict([d.items()[0] for d in t2]).items()]
100 loops, best of 3: 11.4 ms per loop
# 用iter,会更好
[{k:v} for k,v in dict([d.items()[0] for d in t2]).iteritems()]
100 loops, best of 3: 10.7 ms per loop