Skip to content

Commit 2bfa6d2

Browse files
committed
Let's make a giant string: Use %timeit for measuring time
Fixes satwikkansal#106
1 parent 9ea7ab0 commit 2bfa6d2

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

README.md

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,40 +2450,47 @@ def convert_list_to_string(l, iters):
24502450
```
24512451
24522452
**Output:**
2453+
24532454
```py
2454-
>>> timeit(add_string_with_plus(10000))
2455-
1000 loops, best of 3: 972 µs per loop
2456-
>>> timeit(add_bytes_with_plus(10000))
2457-
1000 loops, best of 3: 815 µs per loop
2458-
>>> timeit(add_string_with_format(10000))
2459-
1000 loops, best of 3: 508 µs per loop
2460-
>>> timeit(add_string_with_join(10000))
2461-
1000 loops, best of 3: 878 µs per loop
2462-
>>> l = ["xyz"]*10000
2463-
>>> timeit(convert_list_to_string(l, 10000))
2464-
10000 loops, best of 3: 80 µs per loop
2455+
# Executed in ipython shell using %timeit for better readablity of results.
2456+
# You can also use the timeit module in normal python shell/scriptm=, example usage below
2457+
# timeit.timeit('add_string_with_plus(10000)', number=1000, globals=globals())
2458+
2459+
>>> NUM_ITERS = 1000
2460+
>>> %timeit -n1000 add_string_with_plus(NUM_ITERS)
2461+
124 µs ± 4.73 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
2462+
>>> %timeit -n1000 add_bytes_with_plus(NUM_ITERS)
2463+
211 µs ± 10.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2464+
>>> %timeit -n1000 add_string_with_format(NUM_ITERS)
2465+
61 µs ± 2.18 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2466+
>>> %timeit -n1000 add_string_with_join(NUM_ITERS)
2467+
117 µs ± 3.21 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2468+
>>> l = ["xyz"]*NUM_ITERS
2469+
>>> %timeit -n1000 convert_list_to_string(l, NUM_ITERS)
2470+
10.1 µs ± 1.06 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
24652471
```
24662472
24672473
Let's increase the number of iterations by a factor of 10.
24682474
24692475
```py
2470-
>>> timeit(add_string_with_plus(100000)) # Linear increase in execution time
2471-
100 loops, best of 3: 9.75 ms per loop
2472-
>>> timeit(add_bytes_with_plus(100000)) # Quadratic increase
2473-
1000 loops, best of 3: 974 ms per loop
2474-
>>> timeit(add_string_with_format(100000)) # Linear increase
2475-
100 loops, best of 3: 5.25 ms per loop
2476-
>>> timeit(add_string_with_join(100000)) # Linear increase
2477-
100 loops, best of 3: 9.85 ms per loop
2478-
>>> l = ["xyz"]*100000
2479-
>>> timeit(convert_list_to_string(l, 100000)) # Linear increase
2480-
1000 loops, best of 3: 723 µs per loop
2476+
>>> NUM_ITERS = 10000
2477+
>>> %timeit -n1000 add_string_with_plus(NUM_ITERS) # Linear increase in execution time
2478+
1.26 ms ± 76.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2479+
>>> %timeit -n1000 add_bytes_with_plus(NUM_ITERS) # Quadratic increase
2480+
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)
24812488
```
24822489
24832490
#### 💡 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.
24852492
- 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).
24872494
- Or better, if already you've contents available in the form of an iterable object, then use `''.join(iterable_object)` which is much faster.
24882495
- `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.
24892496
```py
@@ -2493,10 +2500,10 @@ Let's increase the number of iterations by a factor of 10.
24932500
s = s + "x" + "y" + "z"
24942501
assert len(s) == 3*iters
24952502
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)
25002507
```
25012508
25022509
---

0 commit comments

Comments
 (0)