|
| 1 | +With Python, concatenating strings with `+=` in a loop creates many intermediate strings, leading to high memory usage and performance degradation. Prefer using f-strings (formatted string literals) or `str.join()` for better performance and lower environmental impact. |
| 2 | + |
| 3 | +== Non Compliant Code Example |
| 4 | + |
| 5 | +[source,python] |
| 6 | +---- |
| 7 | +city = "New York" |
| 8 | +street = "5th Avenue" |
| 9 | +zip_code = "10001" |
| 10 | +address = "" |
| 11 | +address += city + ", " + street + zip_code # Noncompliant: inefficient string concatenation |
| 12 | +---- |
| 13 | + |
| 14 | +In this example, the `+=` operation creates a new string object for each concatenation. When done repeatedly (e.g., in loops), this leads to excessive memory allocations and slower performance. |
| 15 | + |
| 16 | +== Compliant Solution |
| 17 | + |
| 18 | +[source,python] |
| 19 | +---- |
| 20 | +# Using f-string for readability and performance |
| 21 | +city = "New York" |
| 22 | +street = "5th Avenue" |
| 23 | +address = f"{city}, {street}" |
| 24 | +# or using str.join() for multiple string concatenations |
| 25 | +parts = ["New York", "5th Avenue", "10001"] |
| 26 | +address = ", ".join(parts) |
| 27 | +---- |
| 28 | + |
| 29 | +These approaches avoid the repeated creation of new string objects. `str.join()` builds the final string in a single operation, and f-strings are compiled into efficient bytecode. |
| 30 | + |
| 31 | +== Relevance Analysis |
| 32 | + |
| 33 | +This rule applies to any Python application performing repeated or large-scale string concatenation (e.g., log generation, data serialization, HTML template generation). |
| 34 | + |
| 35 | +=== Configuration |
| 36 | + |
| 37 | +* Processor: Intel(R) Core(TM) Ultra 5 135U, 2100 MHz, 12 cores, 14 logical processors |
| 38 | +* RAM: 16 GB |
| 39 | +* CO2 Emissions Measurement: Using CodeCarbon |
| 40 | + |
| 41 | +=== Context |
| 42 | + |
| 43 | +Two string-building techniques were benchmarked: |
| 44 | +- **Non-compliant:** `+=` string concatenation in a loop |
| 45 | +- **Compliant:** `str.join()` or f-string formatting |
| 46 | + |
| 47 | +Metrics assessed: |
| 48 | +- Execution time |
| 49 | +- Carbon emissions |
| 50 | + |
| 51 | +=== Impact Analysis |
| 52 | + |
| 53 | +image::concat.png[] |
| 54 | + |
| 55 | +- *CO₂ Emissions:* Reduced by over **10×** when using `str.join()` instead of `+=` |
| 56 | +- *Energy Efficiency:* Significantly improved due to lower memory allocations and faster execution |
| 57 | + |
| 58 | +These results demonstrate that even small code patterns, like string concatenation, can have a measurable impact when scaled in production environments. |
| 59 | + |
| 60 | +== Conclusion |
| 61 | + |
| 62 | +Replacing `+=` in string concatenation with `f-strings` or `str.join()`: |
| 63 | +- Reduces memory overhead |
| 64 | +- Improves runtime performance |
| 65 | +- Decreases CO₂ emissions |
| 66 | + |
| 67 | +== References |
| 68 | +- https://wiki.python.org/moin/PythonSpeed/PerformanceTips |
0 commit comments