python字符连接效率有关问题
python字符连接效率问题
win32com.client打开excel,excel保存的是数据库table数据,用的是下面的字符连接方式,也试过先append成列表再join方法,直接+=方法,‘%s,%s’%(xx,xx)方法,发现效率都不高,十几个字段的表1500多记录要5分钟左右,有一个用perl写的版本处理同样的只需要30秒左右,perl用的连接字符方式是.=.本人最近工作没那么忙,在弄下python,想弄个python版本的导数据库小工具(已有perl版,但本人不懂perl),但发现字符连接耗时太多,导至整个过程长达1个多钟,没法忍受。求教下大家有没提高的方法。或者改进这段代码。感觉写的比较乱。最近才学python的
row = sh.UsedRange.Rows.Count
col = sh.UsedRange.Columns.Count
for r in range(2, row + 1):
sql_s = StringIO()
sql_s.write(sql_str)
cant_do = 0
for c in range(1, col + 1):
var = sh.Cells(r, c).Value
if (var == None):
if (c == 1):
cant_do = 1
break
elif (c <= real_col):
sql_s.write(',\'\'')
else:
if (isinstance(var, unicode)):
var = var.encode('gb18030')
if (c == 1):
sql_s.write('\'%s\'' % var)
else:
sql_s.write(',\'%s\'' % var)
------解决方案--------------------
+的效率确实很低
如果是顺序连接,最佳方法是建一个StringIO,逐个写入最后getvalue()读出
非顺序连接(有可能前插入),就用format()或''.join(字串列表)
------解决方案--------------------
不是说python的字符串是immutable对象吗?
用字符串列表试试,就是'abc'弄成['a','b','c']这种
不过,详细的测试看着一页:
http://www.skymind.com/~ocrow/python_string/
Method 1: Naive appending
Method 2: MutableString class
Method 3: Character arrrays
Method 4: Build a list of strings, then join it
Method 5: Write to a pseudo file
Method 6: List comprehensions
结论是:
Results: Twenty thousand integers
Concatenations(/s) Process size(kB)
Method 1 3770 2424
Method 2 2230 2424
Method 3 29,600 2452
Method 4 83,700 3028
Method 5 90,900 2536
Method 6 119,800 3000
Results: Five hundred thousand integers
Concatenations(/s) Process size(kB)
Method 3 17,100 8,016
Method 4 74,800 22,872
Method 5 94,900 10,480
Method 6 102,100 22,844
win32com.client打开excel,excel保存的是数据库table数据,用的是下面的字符连接方式,也试过先append成列表再join方法,直接+=方法,‘%s,%s’%(xx,xx)方法,发现效率都不高,十几个字段的表1500多记录要5分钟左右,有一个用perl写的版本处理同样的只需要30秒左右,perl用的连接字符方式是.=.本人最近工作没那么忙,在弄下python,想弄个python版本的导数据库小工具(已有perl版,但本人不懂perl),但发现字符连接耗时太多,导至整个过程长达1个多钟,没法忍受。求教下大家有没提高的方法。或者改进这段代码。感觉写的比较乱。最近才学python的
row = sh.UsedRange.Rows.Count
col = sh.UsedRange.Columns.Count
for r in range(2, row + 1):
sql_s = StringIO()
sql_s.write(sql_str)
cant_do = 0
for c in range(1, col + 1):
var = sh.Cells(r, c).Value
if (var == None):
if (c == 1):
cant_do = 1
break
elif (c <= real_col):
sql_s.write(',\'\'')
else:
if (isinstance(var, unicode)):
var = var.encode('gb18030')
if (c == 1):
sql_s.write('\'%s\'' % var)
else:
sql_s.write(',\'%s\'' % var)
------解决方案--------------------
+的效率确实很低
如果是顺序连接,最佳方法是建一个StringIO,逐个写入最后getvalue()读出
非顺序连接(有可能前插入),就用format()或''.join(字串列表)
------解决方案--------------------
不是说python的字符串是immutable对象吗?
用字符串列表试试,就是'abc'弄成['a','b','c']这种
不过,详细的测试看着一页:
http://www.skymind.com/~ocrow/python_string/
Method 1: Naive appending
Method 2: MutableString class
Method 3: Character arrrays
Method 4: Build a list of strings, then join it
Method 5: Write to a pseudo file
Method 6: List comprehensions
结论是:
Results: Twenty thousand integers
Concatenations(/s) Process size(kB)
Method 1 3770 2424
Method 2 2230 2424
Method 3 29,600 2452
Method 4 83,700 3028
Method 5 90,900 2536
Method 6 119,800 3000
Results: Five hundred thousand integers
Concatenations(/s) Process size(kB)
Method 3 17,100 8,016
Method 4 74,800 22,872
Method 5 94,900 10,480
Method 6 102,100 22,844