4000-520-616
欢迎来到免疫在线!(蚂蚁淘生物旗下平台)  请登录 |  免费注册 |  询价篮
主营:原厂直采,平行进口,授权代理(蚂蚁淘为您服务)
咨询热线电话
4000-520-616
当前位置: 首页 > 新闻动态 >
热卖商品
新闻详情
GitHub - eternallovelin/wtfpython-cn: wtfpython的中文翻译/...
来自 : github 发布时间:2021-03-25

Let\'s make a giant string!/来做个巨大的字符串吧!

def add_string_with_plus(iters): s = \"\" for i in range(iters): s += \"xyz\" assert len(s) == 3*itersdef add_bytes_with_plus(iters): s = b\"\" for i in range(iters): s += b\"xyz\" assert len(s) == 3*itersdef add_string_with_format(iters): fs = \"{}\"*iters s = fs.format(*([\"xyz\"]*iters)) assert len(s) == 3*itersdef add_string_with_join(iters): l = [] for i in range(iters): l.append(\"xyz\") s = \"\".join(l) assert len(s) == 3*itersdef convert_list_to_string(l, iters): s = \"\".join(l) assert len(s) == 3*iters

Output:

 timeit(add_string_with_plus(10000))1000 loops, best of 3: 972 µs per loop timeit(add_bytes_with_plus(10000))1000 loops, best of 3: 815 µs per loop timeit(add_string_with_format(10000))1000 loops, best of 3: 508 µs per loop timeit(add_string_with_join(10000))1000 loops, best of 3: 878 µs per loop l = [\"xyz\"]*10000 timeit(convert_list_to_string(l, 10000))10000 loops, best of 3: 80 µs per loop

让我们将迭代次数增加10倍.

 timeit(add_string_with_plus(100000)) # 执行时间线性增加100 loops, best of 3: 9.75 ms per loop timeit(add_bytes_with_plus(100000)) # 二次增加1000 loops, best of 3: 974 ms per loop timeit(add_string_with_format(100000)) # 线性增加100 loops, best of 3: 5.25 ms per loop timeit(add_string_with_join(100000)) # 线性增加100 loops, best of 3: 9.85 ms per loop l = [\"xyz\"]*100000 timeit(convert_list_to_string(l, 100000)) # 线性增加1000 loops, best of 3: 723 µs per loop

本文链接: http://eternalnice.immuno-online.com/view-761088.html

发布于 : 2021-03-25 阅读(0)
公司介绍
品牌分类
联络我们
服务热线:4000-520-616
(限工作日9:00-18:00)
QQ :1570468124
手机:18915418616