Skip to content

Instantly share code, notes, and snippets.

@kain-jy
Created April 6, 2013 11:18
Show Gist options
  • Save kain-jy/5325775 to your computer and use it in GitHub Desktop.
Save kain-jy/5325775 to your computer and use it in GitHub Desktop.
[python] list結合の検証
# coding: utf-8
import profile
#from memory_profiler import profile as mprofile
from random import random
from itertools import chain
data = [[random() for j in xrange(1000)] for i in xrange(1000)]
def add(x,y):
return x+y
#@mprofile
def profile_reduce_lambda():
'''lambdaを使った場合'''
return reduce(lambda x,y: x+y, data)
#@mprofile
def profile_reduce_add():
'''lambdaを使わずadd()をあらかじめ定義'''
return reduce(add, data)
#@mprofile
def profile_chain():
return chain(*data)
#@mprofile
def profile_append():
res = []
for i in data:
for j in i:
res.append(j)
return res
profile.run('profile_reduce_lambda()')
profile.run('profile_reduce_add()')
profile.run('profile_chain()')
profile.run('profile_append()')
#if __name__=="__main__":
# profile_reduce_lambda()
# profile_reduce_add()
# profile_chain()
# profile_append()
ilename: run.py
Line # Mem usage Increment Line Contents
================================================
13 @mprofile
14 41.012 MB 0.000 MB def profile_reduce_lambda():
15 170.035 MB 129.023 MB return reduce(lambda x,y: x+y, data)
Filename: run.py
Line # Mem usage Increment Line Contents
================================================
17 @mprofile
18 162.555 MB 0.000 MB def profile_reduce_add():
19 170.184 MB 7.629 MB return reduce(add, data)
Filename: run.py
Line # Mem usage Increment Line Contents
================================================
21 @mprofile
22 162.680 MB 0.000 MB def profile_chain():
23 162.680 MB 0.000 MB return chain(*data)
Filename: run.py
Line # Mem usage Increment Line Contents
================================================
25 @mprofile
26 162.680 MB 0.000 MB def profile_append():
27 162.680 MB 0.000 MB res = []
28 162.727 MB 0.047 MB for i in data:
29 196.203 MB 33.477 MB for j in i:
30 187.859 MB -8.344 MB res.append(j)
31 187.859 MB 0.000 MB return res
1004 function calls in 7.695 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 3.185 3.185 7.682 7.682 :0(reduce)
1 0.006 0.006 0.006 0.006 :0(setprofile)
1 0.006 0.006 7.689 7.689 <string>:1(<module>)
1 0.000 0.000 7.695 7.695 profile:0(profile_reduce_lambda())
0 0.000 0.000 profile:0(profiler)
1 0.000 0.000 7.682 7.682 run.py:14(profile_reduce_lambda)
999 4.497 0.005 4.497 0.005 run.py:15(<lambda>)
1004 function calls in 7.631 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 3.157 3.157 7.624 7.624 :0(reduce)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.006 0.006 7.631 7.631 <string>:1(<module>)
1 0.000 0.000 7.631 7.631 profile:0(profile_reduce_add())
0 0.000 0.000 profile:0(profiler)
999 4.468 0.004 4.468 0.004 run.py:10(add)
1 0.000 0.000 7.624 7.624 run.py:18(profile_reduce_add)
4 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 profile:0(profile_chain())
0 0.000 0.000 profile:0(profiler)
1 0.000 0.000 0.000 0.000 run.py:22(profile_chain)
1000004 function calls in 5.369 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1000000 2.557 0.000 2.557 0.000 :0(append)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.009 0.009 5.369 5.369 <string>:1(<module>)
1 0.000 0.000 5.369 5.369 profile:0(profile_append())
0 0.000 0.000 profile:0(profiler)
1 2.803 2.803 5.360 5.360 run.py:26(profile_append)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment