python三种字符串拼接效率对比

#coding=utf8

import cStringIO
import StringIO
import time
values = []

items = ['1,2,31,2,31,2,31,2,3'] * 10000000
#print items


st = float(time.time())
for i in items:
    values.append(i)

news = ''.join(values)
print len(news)

print 'list append cost time:%s'%(float(time.time()) - st)

strio = cStringIO.StringIO()
st = float(time.time())
for j in items:
    strio.write(j)

print len(strio.getvalue())
print 'cStringIO cost time:%s'%(float(time.time()) - st)


strio = StringIO.StringIO()
st = float(time.time())
for j in items:
    strio.write(j)

print len(strio.getvalue())
print 'StringIO cost time:%s'%(float(time.time()) - st)

posted @ 2013-04-12 16:44  践道者  阅读(11364)  评论(0编辑  收藏  举报