Commit 4b791b4
authored
⚡️ Speed up function
The optimization transforms **sequential async execution into concurrent execution** using `asyncio.gather()`.
**Key Changes:**
- **Original**: Awaits each `fake_api_call` one at a time in a loop, creating a sequential bottleneck
- **Optimized**: Creates all tasks upfront in a list comprehension, then uses `asyncio.gather(*tasks)` to run them concurrently
**Why This is Faster:**
The original code has a fundamental async anti-pattern - it waits for each API call to complete before starting the next one, negating the benefits of async programming. With random delays of 0.5-2.0 seconds per call, the total runtime grows linearly with the number of URLs.
The optimized version leverages async concurrency properly by:
1. Creating all coroutine tasks immediately (no blocking)
2. Using `asyncio.gather()` to execute them simultaneously
3. Waiting only for the slowest task to complete, not the sum of all tasks
**Performance Impact:**
- **47% speedup** demonstrates the power of proper async concurrency
- Line profiler shows the optimized version processes many more calls (1030 vs 45 hits) in similar total time, indicating concurrent execution
- Most effective for workloads with multiple I/O-bound operations that can run independently
**Test Case Performance:**
This optimization excels with larger URL lists where the concurrent execution advantage multiplies, while maintaining identical correctness for all edge cases including empty lists, special characters, and mixed data types.some_api_call by 48%1 parent c373405 commit 4b791b4
1 file changed
+63
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
0 commit comments