import time time01 = time.time() a = "" for i in range(1000000): a += "six" time02 = time.time() print("时间差:" +str(time02-time01)) time03 = time.time() list1 = [] for i in range(1000000): list1.append("six") a = "".join(list1) time04 = time.time() print("时间差:" +str(time04-time03))
运算结果:join 效率大于运算符效率
因为运算符会生成新的字符串对象,会创建很多次。
join仅创建1次对象