코드
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from itertools import izip, islice
# normal loop
def using_loop(ary):
result = []
for i in range(len(ary) - 1):
result.append((ary[1], ary[i + 1]))
return result
# built-in map()
def using_map(ary):
return map(None, ary[:-1], ary[1:])
# built-in zip() 을 사용하는 방법
def using_zip(ary):
return zip(ary, ary[1:])
# itertools를 사용하는 방법
def using_izip(ary):
return [(x, y) for x, y in izip(ary, islice(ary, 1, None))]
# generator를 사용하는 방법
def using_gen(ary):
return [interval for interval in intervals(ary)]
def intervals(it):
it = iter(it)
st = it.next()
for en in it:
yield (st, en)
st = en
def doit():
SRCLIST = range(1, 1000000)
using_loop(SRCLIST)
using_map(SRCLIST)
using_zip(SRCLIST)
using_izip(SRCLIST)
using_gen(SRCLIST)
if __name__ == '__main__':
import profile
out = 'tmp.prof'
profile.run('doit()', out)
import pstats
profObj = pstats.Stats(out)
profObj.sort_stats('cumulative').print_stats()
결과
range(1, 100)
Fri Dec 01 14:00:29 2006 tmp.prof 212 function calls in 0.017 CPU seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.001 0.001 0.017 0.017 profile:0(doit()) 1 0.000 0.000 0.015 0.015 <string>:1(?) 1 0.000 0.000 0.015 0.015 C:\speedtest2.py:35(doit) 1 0.014 0.014 0.014 0.014 C:\speedtest2.py:23(using_izip) 1 0.000 0.000 0.001 0.001 C:\speedtest2.py:26(using_gen) 1 0.000 0.000 0.001 0.001 C:\speedtest2.py:10(using_loop) 1 0.000 0.000 0.000 0.000 :0(setprofile) 99 0.000 0.000 0.000 0.000 C:\speedtest2.py:28(intervals) 98 0.000 0.000 0.000 0.000 :0(append) 1 0.000 0.000 0.000 0.000 C:\speedtest2.py:17(using_map) 1 0.000 0.000 0.000 0.000 C:\speedtest2.py:20(using_zip) 1 0.000 0.000 0.000 0.000 :0(map) 2 0.000 0.000 0.000 0.000 :0(range) 1 0.000 0.000 0.000 0.000 :0(zip) 1 0.000 0.000 0.000 0.000 :0(iter) 1 0.000 0.000 0.000 0.000 :0(len) 0 0.000 0.000 profile:0(profiler)
zip() - map() - for loop - generator - izip() 순으로 빠르다.
range(1, 10000)
Fri Dec 01 13:57:18 2006 tmp.prof 20012 function calls in 0.157 CPU seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.157 0.157 profile:0(doit()) 1 0.000 0.000 0.156 0.156 <string>:1(?) 1 0.003 0.003 0.156 0.156 C:\speedtest2.py:35(doit) 1 0.047 0.047 0.075 0.075 C:\speedtest2.py:10(using_loop) 1 0.032 0.032 0.067 0.067 C:\speedtest2.py:26(using_gen) 9999 0.035 0.000 0.035 0.000 C:\speedtest2.py:28(intervals) 9998 0.028 0.000 0.028 0.000 :0(append) 1 0.004 0.004 0.004 0.004 C:\speedtest2.py:23(using_izip) 1 0.000 0.000 0.003 0.003 C:\speedtest2.py:17(using_map) 1 0.003 0.003 0.003 0.003 :0(map) 1 0.000 0.000 0.003 0.003 C:\speedtest2.py:20(using_zip) 1 0.003 0.003 0.003 0.003 :0(zip) 1 0.001 0.001 0.001 0.001 :0(setprofile) 2 0.001 0.000 0.001 0.000 :0(range) 1 0.000 0.000 0.000 0.000 :0(iter) 1 0.000 0.000 0.000 0.000 :0(len) 0 0.000 0.000 profile:0(profiler)
zip() - map() - izip() - generator - for loop 순으로 빠르다.
range(1, 1000000)
Fri Dec 01 13:55:11 2006 tmp.prof 2000012 function calls in 22.758 CPU seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 22.758 22.758 profile:0(doit()) 1 0.020 0.020 22.757 22.757 <string>:1(?) 1 0.299 0.299 22.737 22.737 C:\speedtest2.py:35(doit) 1 5.855 5.855 8.752 8.752 C:\speedtest2.py:10(using_loop) 1 3.322 3.322 7.997 7.997 C:\speedtest2.py:26(using_gen) 999999 4.675 0.000 4.675 0.000 C:\speedtest2.py:28(intervals) 999998 2.862 0.000 2.862 0.000 :0(append) 1 0.065 0.065 2.213 2.213 C:\speedtest2.py:17(using_map) 1 2.148 2.148 2.148 2.148 :0(map) 1 0.033 0.033 1.893 1.893 C:\speedtest2.py:20(using_zip) 1 1.860 1.860 1.860 1.860 :0(zip) 1 1.549 1.549 1.549 1.549 C:\speedtest2.py:23(using_izip) 2 0.070 0.035 0.070 0.035 :0(range) 1 0.001 0.001 0.001 0.001 :0(setprofile) 1 0.000 0.000 0.000 0.000 :0(iter) 1 0.000 0.000 0.000 0.000 :0(len) 0 0.000 0.000 profile:0(profiler)
izip() - zip() - map() - generator - for loop 순으로 빠르다.