Skip to content

Instantly share code, notes, and snippets.

@mumumu
Last active August 29, 2015 14:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mumumu/5e55b3a8c8d4eb29d73e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from benchmarker import Benchmarker
from collections import Counter
with Benchmarker(width=20) as bm:
counters = [Counter(), Counter(), Counter(), Counter()]
with bm('double loop'):
for i in xrange(10000000):
for counter in counters:
counter['hoge'] += 1
counter1 = Counter()
counter2 = Counter()
counter3 = Counter()
counter4 = Counter()
with bm('single loop'):
for i in xrange(10000000):
counter1['hoge'] += 1
counter2['hoge'] += 1
counter3['hoge'] += 1
counter4['hoge'] += 1
counter4 = Counter()
counter5 = Counter()
counter6 = Counter()
counter7 = Counter()
def count():
counter4['hoge'] += 1
counter5['hoge'] += 1
counter6['hoge'] += 1
counter7['hoge'] += 1
with bm('function invoke'):
for i in xrange(10000000):
count()
def count(c):
c['hoge'] += 1
with bm('double loop with map'):
for i in xrange(10000000):
map(count, counters)
@mumumu
Copy link
Author

mumumu commented Aug 20, 2014

インライン展開みたいなシンプルなテクニックが通用する結果に
map 遅すぎワロタ

## benchmarker:       release 3.0.1 (for python)                                                                                                                                                                                                              
## python platform:   linux2 [GCC 4.8.2]                                                                                                                                                                                                                      
## python version:    2.7.6                                                                                                                                                                                                                                                               
## python executable: /home/mumumu/benchmark/bin/python                                                                                                                                                                                                                                   

##                       user       sys     total      real                                                                                                                                                                                                                               
double loop           13.6900    0.0200   13.7100   13.7069                                                                                                                                                                                                                                                                  
single loop           12.2100    0.0300   12.2400   12.2302                                                                                                                                                                                                                                                                  
function invoke       12.7300    0.0100   12.7400   12.7385                                                                                                                                                                                                                                                                  
double loop with map  17.4600    0.0400   17.5000   17.4840

## Ranking               real
single loop           12.2302 (100.0%) *************************
function invoke       12.7385 ( 96.0%) ************************
double loop           13.7069 ( 89.2%) **********************
double loop with map  17.4840 ( 70.0%) *****************

## Ratio Matrix          real    [01]    [02]    [03]    [04]
[01] single loop      12.2302  100.0%  104.2%  112.1%  143.0%
[02] function invoke  12.7385   96.0%  100.0%  107.6%  137.3%
[03] double loop      13.7069   89.2%   92.9%  100.0%  127.6%
[04] double loop with map  17.4840   70.0%   72.9%   78.4%  100.0%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment