Skip to content

Commit 3c64b48

Browse files
author
ducheng
committed
EX.Let's make a giant string
1 parent 6a17caf commit 3c64b48

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

README.md

Lines changed: 15 additions & 15 deletions
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)
@@ -2183,7 +2183,7 @@ True
21832183
21842184
---
21852185
2186-
### > Let's make a giant string!
2186+
### > Let's make a giant string!/来做个巨大的字符串吧!
21872187
21882188
```py
21892189
def add_string_with_plus(iters):
@@ -2230,28 +2230,28 @@ def convert_list_to_string(l, iters):
22302230
10000 loops, best of 3: 80 µs per loop
22312231
```
22322232
2233-
Let's increase the number of iterations by a factor of 10.
2233+
让我们将迭代次数增加10倍.
22342234
22352235
```py
2236-
>>> timeit(add_string_with_plus(100000)) # Linear increase in execution time
2236+
>>> timeit(add_string_with_plus(100000)) # 执行时间线性增加
22372237
100 loops, best of 3: 9.75 ms per loop
2238-
>>> timeit(add_bytes_with_plus(100000)) # Quadratic increase
2238+
>>> timeit(add_bytes_with_plus(100000)) # 二次增加
22392239
1000 loops, best of 3: 974 ms per loop
2240-
>>> timeit(add_string_with_format(100000)) # Linear increase
2240+
>>> timeit(add_string_with_format(100000)) # 线性增加
22412241
100 loops, best of 3: 5.25 ms per loop
2242-
>>> timeit(add_string_with_join(100000)) # Linear increase
2242+
>>> timeit(add_string_with_join(100000)) # 线性增加
22432243
100 loops, best of 3: 9.85 ms per loop
22442244
>>> l = ["xyz"]*100000
2245-
>>> timeit(convert_list_to_string(l, 100000)) # Linear increase
2245+
>>> timeit(convert_list_to_string(l, 100000)) # 线性增加
22462246
1000 loops, best of 3: 723 µs per loop
22472247
```
22482248
2249-
#### 💡 Explanation
2250-
- 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.
2251-
- 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)
2252-
- Therefore, it's advised to use `.format.` or `%` syntax (however, they are slightly slower than `+` for short strings).
2253-
- Or better, if already you've contents available in the form of an iterable object, then use `''.join(iterable_object)` which is much faster.
2254-
- `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.
2249+
#### 💡 说明:
2250+
- 你可以在这获得更多 [timeit](https://docs.python.org/3/library/timeit.html) 的相关信息. 它通常用于衡量代码片段的执行时间.
2251+
- 不要用 `+` 去生成过长的字符串, 在 Python, `str` 是不可变得, 所以在每次连接中你都要把左右两个字符串复制到新的字符串中. 如果你连接四个长度为10的字符串, 你需要拷贝 (10+10) + ((10+10)+10) + (((10+10)+10)+10) = 90 个字符而不是 40 个字符. 随着字符串的数量和大小的增加, 情况会变得越发的糟糕 (就像`add_bytes_with_plus` 函数的执行时间一样)
2252+
- 因此, 更建议使用 `.format.` `%` 语法 (但是, 对于短字符串, 它们比 `+` 稍慢一点).
2253+
- 又或者, 如果你所需的内容已经以可迭代对象的形式提供了, 使用 `''.join(可迭代对象)` 要快多了.
2254+
- `add_string_with_plus` 的执行时间没有像 `add_bytes_with_plus` 一样出现二次增加是因为解释器会如同上一个列子所讨论的一样优化 `+=`. 用 `s = s + "x" + "y" + "z"` 替代 `s += "xyz"` 的话, 执行时间就会二次增加了.
22552255
```py
22562256
def add_string_with_plus(iters):
22572257
s = ""
@@ -2261,7 +2261,7 @@ Let's increase the number of iterations by a factor of 10.
22612261
22622262
>>> timeit(add_string_with_plus(10000))
22632263
100 loops, best of 3: 9.87 ms per loop
2264-
>>> timeit(add_string_with_plus(100000)) # Quadratic increase in execution time
2264+
>>> timeit(add_string_with_plus(100000)) # 执行时间二次增加
22652265
1 loops, best of 3: 1.09 s per loop
22662266
```
22672267

0 commit comments

Comments
 (0)