2017.3.13 update
- @shiracamus さん @tenmyo さん にコメント頂いた処理を追加検証
- 結果が変わったので修正
if文書くのやだ
こういうの
if key not in test_dict:
test_dict[key] = 0
test_dict[key] += 1
こう書きたい
test_dict[key] = test_dict.get(key, 0) + count
速度はかってみる
遅かったらこわいものね
# coding=utf-8
import random
import time
import collections
S = "abcdefghijklmnopqrstuvwxyz01234567890"
sum_map = dict()
def get_rand_key():
return "".join([random.choice(S) for i in range(3)])
def test01(max=10000):
count = 0
test_dict = dict()
while count <= max:
count += 1
key = get_rand_key()
if key not in test_dict:
test_dict[key] = 0
test_dict[key] += count
def test02(max=10000):
count = 0
test_dict = dict()
while count <= max:
count += 1
key = get_rand_key()
test_dict[key] = test_dict.get(key, 0) + count
def test03(max=10000):
count = 0
test_dict = collections.defaultdict(int)
while count <= max:
count += 1
key = get_rand_key()
test_dict[key] += count
def test04(max=10000):
count = 0
test_dict = collections.Counter()
while count <= max:
count += 1
key = get_rand_key()
test_dict[key] += count
def check(method, num, loop_num):
name = method.__name__
start_ts = time.time()
method(max=num)
diff = time.time() - start_ts
sum_map[name] = sum_map.get(name, 0) + diff
print "[{}] {} {}sec".format(loop_num, name, diff)
for i in range(20):
num = 1000000
check(test01, num, i)
check(test02, num, i)
check(test03, num, i)
check(test04, num, i)
for key in sorted(sum_map.keys()):
print key, sum_map[key]
あんまり変わらない defaultdict
が早そう!
なんどか実行してみたけど、大きな差はあまり無さそう。
僕は短い方で書いて生きていこうと思います。
@shiracamus さんにコメント頂いた defaultdict
での計測が早そうです。
試しに3回試してみても同じ結果となりました。
if あり |
.get |
defaultdict |
Counter |
---|---|---|---|
50.5 | 52.6 | 49.3 | 55.4 |
53.0 | 53.8 | 50.2 | 56.6 |
53.5 | 53.7 | 49.9 | 54.7 |
今度 defaultdict
使っていくようにしよう!
もやりポイント
何か特定状況下でどちらがめっちゃ早いとかありそう。
hitの割合によっても変わりそう。いろんなパターンありそう…
だれか知ってる人いらっしゃったら教えてください
Counter
はなぜ遅いんだろう。使い方違うのかしら。