Python中的两种返回对象的字符串函数效率测试: repr() vs str()

2011-03-12  李琪 

Test result is that repr() is faster than str() about 4 seconds as the same larger data.
========Test Result =========
No.1000000010000001
Method 1 costs 10.7029998302 seconds
No.1000000010000001
Method 2 costs 14.3920001984 seconds
========Test Result =========
Below is its test script:
Created on 2011-3-12
@author email: liqi1031@gmail.com
'''
import time
#Test the repr(), when doing string plus operation.
startTime = time.time()
a = 1
while a <= 10000000:
    s = 'No.' + repr(a)
    a = a + 1
    s = s + repr(a)
print s
costTime = time.time() - startTime
print 'Method 1 costs %s seconds' % costTime
#Test the str(), when doing string plus operation.
startTime = time.time()
a = 1
while a <= 10000000:
    s = 'No.' + str(a)
    a = a + 1
    s = s + str(a)
print s
costTime = time.time() - startTime
print 'Method 2 costs %s seconds' % costTime
401°/4003 人阅读/1 条评论 发表评论

张东升  2011-03-15

怎么不把结果分享一下呢


登录 后发表评论