2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

dictのkeyの存在確認してから処理する時にif文書きたくないんです

Last updated at Posted at 2017-03-11

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 はなぜ遅いんだろう。使い方違うのかしら。

2
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?