Skip to content

Commit c5dba5b

Browse files
duchengleisurelicht
ducheng
authored andcommitted
EX.Let's make a giant string
1 parent be0dfea commit c5dba5b

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

README.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ So, here we go...
7777
- [> Mangling time!修饰时间! *](#-mangling-time修饰时间-)
7878
- [Section: Miscellaneous](#section-miscellaneous)
7979
- [> `+=` is faster/更快的 `+=` ](#--is-faster更快的-)
80-
- [> Let's make a giant string!](#-lets-make-a-giant-string)
80+
- [> Let's make a giant string!/来做个巨大的字符串吧!](#-lets-make-a-giant-string来做个巨大的字符串吧)
8181
- [> Explicit typecast of strings](#-explicit-typecast-of-strings)
8282
- [> Minor Ones](#-minor-ones)
8383
- [Contributing](#contributing)
@@ -2188,7 +2188,7 @@ True
21882188
21892189
---
21902190
2191-
### > Let's make a giant string!
2191+
### > Let's make a giant string!/来做个巨大的字符串吧!
21922192
21932193
```py
21942194
def add_string_with_plus(iters):
@@ -2235,28 +2235,28 @@ def convert_list_to_string(l, iters):
22352235
10000 loops, best of 3: 80 µs per loop
22362236
```
22372237
2238-
Let's increase the number of iterations by a factor of 10.
2238+
让我们将迭代次数增加10倍.
22392239
22402240
```py
2241-
>>> timeit(add_string_with_plus(100000)) # Linear increase in execution time
2241+
>>> timeit(add_string_with_plus(100000)) # 执行时间线性增加
22422242
100 loops, best of 3: 9.75 ms per loop
2243-
>>> timeit(add_bytes_with_plus(100000)) # Quadratic increase
2243+
>>> timeit(add_bytes_with_plus(100000)) # 二次增加
22442244
1000 loops, best of 3: 974 ms per loop
2245-
>>> timeit(add_string_with_format(100000)) # Linear increase
2245+
>>> timeit(add_string_with_format(100000)) # 线性增加
22462246
100 loops, best of 3: 5.25 ms per loop
2247-
>>> timeit(add_string_with_join(100000)) # Linear increase
2247+
>>> timeit(add_string_with_join(100000)) # 线性增加
22482248
100 loops, best of 3: 9.85 ms per loop
22492249
>>> l = ["xyz"]*100000
2250-
>>> timeit(convert_list_to_string(l, 100000)) # Linear increase
2250+
>>> timeit(convert_list_to_string(l, 100000)) # 线性增加
22512251
1000 loops, best of 3: 723 µs per loop
22522252
```
22532253
2254-
#### 💡 Explanation
2255-
- You can read more about [timeit](https://docs.python.org/3/library/timeit.html) from here. It is generally used to measure the execution time of snippets.
2256-
- Don't use `+` for generating long strings — In Python, `str` is immutable, so the left and right strings have to be copied into the new string for every pair of concatenations. If you concatenate four strings of length 10, you'll be copying (10+10) + ((10+10)+10) + (((10+10)+10)+10) = 90 characters instead of just 40 characters. Things get quadratically worse as the number and size of the string increases (justified with the execution times of `add_bytes_with_plus` function)
2257-
- Therefore, it's advised to use `.format.` or `%` syntax (however, they are slightly slower than `+` for short strings).
2258-
- Or better, if already you've contents available in the form of an iterable object, then use `''.join(iterable_object)` which is much faster.
2259-
- `add_string_with_plus` didn't show a quadratic increase in execution time unlike `add_bytes_with_plus` because of the `+=` optimizations discussed in the previous example. Had the statement been `s = s + "x" + "y" + "z"` instead of `s += "xyz"`, the increase would have been quadratic.
2254+
#### 💡 说明:
2255+
- 你可以在这获得更多 [timeit](https://docs.python.org/3/library/timeit.html) 的相关信息. 它通常用于衡量代码片段的执行时间.
2256+
- 不要用 `+` 去生成过长的字符串, 在 Python, `str` 是不可变的, 所以在每次连接中你都要把左右两个字符串复制到新的字符串中. 如果你连接四个长度为10的字符串, 你需要拷贝 (10+10) + ((10+10)+10) + (((10+10)+10)+10) = 90 个字符而不是 40 个字符. 随着字符串的数量和大小的增加, 情况会变得越发的糟糕 (就像`add_bytes_with_plus` 函数的执行时间一样)
2257+
- 因此, 更建议使用 `.format.` `%` 语法 (但是, 对于短字符串, 它们比 `+` 稍慢一点).
2258+
- 又或者, 如果你所需的内容已经以可迭代对象的形式提供了, 使用 `''.join(可迭代对象)` 要快多了.
2259+
- `add_string_with_plus` 的执行时间没有像 `add_bytes_with_plus` 一样出现二次增加是因为解释器会如同上一个例子所讨论的一样优化 `+=`. 用 `s = s + "x" + "y" + "z"` 替代 `s += "xyz"` 的话, 执行时间就会二次增加了.
22602260
```py
22612261
def add_string_with_plus(iters):
22622262
s = ""
@@ -2266,7 +2266,7 @@ Let's increase the number of iterations by a factor of 10.
22662266
22672267
>>> timeit(add_string_with_plus(10000))
22682268
100 loops, best of 3: 9.87 ms per loop
2269-
>>> timeit(add_string_with_plus(100000)) # Quadratic increase in execution time
2269+
>>> timeit(add_string_with_plus(100000)) # 执行时间二次增加
22702270
1 loops, best of 3: 1.09 s per loop
22712271
```
22722272

0 commit comments

Comments
 (0)