You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
6.82 ms ± 134 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2481
+
>>> %timeit -n1000 add_string_with_format(NUM_ITERS) # Linear increase
2482
+
645 µs ± 24.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2483
+
>>> %timeit -n1000 add_string_with_join(NUM_ITERS) # Linear increase
2484
+
1.17 ms ± 7.25 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2485
+
>>> l = ["xyz"]*NUM_ITERS
2486
+
>>> %timeit -n1000 convert_list_to_string(l, NUM_ITERS) # Linear increase
2487
+
86.3 µs ± 2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2481
2488
```
2482
2489
2483
2490
#### 💡 Explanation
2484
-
- 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.
2491
+
- You can read more about [timeit](https://docs.python.org/3/library/timeit.html) or [%timeit](https://ipython.org/ipython-doc/dev/interactive/magics.html#magic-timeit) on these links. They are used to measure the execution time of code pieces.
2485
2492
- 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)
2486
-
- Therefore, it's advised to use `.format.` or `%` syntax (however, they are slightly slower than `+` for short strings).
2493
+
- Therefore, it's advised to use `.format.` or `%` syntax (however, they are slightly slower than `+` for very short strings).
2487
2494
- Or better, if already you've contents available in the form of an iterable object, then use `''.join(iterable_object)` which is much faster.
2488
2495
- `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.
2489
2496
```py
@@ -2493,10 +2500,10 @@ Let's increase the number of iterations by a factor of 10.
2493
2500
s = s + "x" + "y" + "z"
2494
2501
assert len(s) == 3*iters
2495
2502
2496
-
>>> timeit(add_string_with_plus(10000))
2497
-
100 loops, best of 3: 9.87 ms per loop
2498
-
>>> timeit(add_string_with_plus(100000)) # Quadratic increase in execution time
2499
-
1 loops, best of 3: 1.09 s per loop
2503
+
>>> %timeit -n100 add_string_with_plus(1000)
2504
+
388 µs ± 22.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2505
+
>>> %timeit -n100 add_string_with_plus(10000) # Quadratic increase in execution time
2506
+
9 ms ± 298 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
0 commit comments