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
Method 1 costs 10.7029998302 seconds
No.1000000010000001
Method 2 costs 14.3920001984 seconds
========Test Result =========
Below is its test script:
#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)
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
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)
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
costTime = time.time() - startTime
print 'Method 2 costs %s seconds' % costTime