Skip to content

Instantly share code, notes, and snippets.

@yono
Created April 21, 2010 14:12
Show Gist options
  • Save yono/373850 to your computer and use it in GitHub Desktop.
Save yono/373850 to your computer and use it in GitHub Desktop.
python2.7で追加されたCounterを試しに使ってみた。従来の辞書との使い方の比較。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import MeCab
from collections import Counter
def func():
m = MeCab.Tagger()
c = Counter()
for i in xrange(100000):
src = "本日は晴天なり"
res = m.parseToNode(src)
while res:
c[res.surface] += 1
res = res.next
for i in c.most_common()[::-1]:
print i
def profile(fname='test.Prof'):
import hotshot
prof = hotshot.Profile(fname)
prof.run('func()')
prof.close
import hotshot.stats
stats = hotshot.stats.load(fname)
stats.strip_dirs()
stats.sort_stats('cumulative')
stats.print_stats(80)
if __name__ == '__main__':
profile()
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import MeCab
def func():
m = MeCab.Tagger()
d = {}
for i in xrange(100000):
src = "本日は晴天なり"
res = m.parseToNode(src)
while res:
d[res.surface] = d.get(res.surface,0) + 1
res = res.next
def profile(fname='test.Prof'):
import hotshot
prof = hotshot.Profile(fname)
prof.run('func()')
prof.close
import hotshot.stats
stats = hotshot.stats.load(fname)
stats.strip_dirs()
stats.sort_stats('cumulative')
stats.print_stats(80)
if __name__ == '__main__':
profile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment