|
| 1 | +--- |
| 2 | +title: Refactoring for environmental sustainability |
| 3 | +shortTitle: Refactor for sustainability |
| 4 | +intro: '{% data variables.copilot.copilot_chat_short %} can suggest ways to make code more environmentally friendly.' |
| 5 | +versions: |
| 6 | + feature: copilot |
| 7 | +category: |
| 8 | + - Refactoring code |
| 9 | +complexity: |
| 10 | + - Simple |
| 11 | +octicon: rocket |
| 12 | +topics: |
| 13 | + - Copilot |
| 14 | +contentType: tutorials |
| 15 | +--- |
| 16 | + |
| 17 | +Code that is inefficient in its use of computational resources can lead to higher energy consumption, which has a negative impact on the environment. Examples of such code include algorithms with high time complexity, excessive memory usage, and unnecessary processing. |
| 18 | + |
| 19 | +{% data variables.copilot.copilot_chat_short %} can help identify inefficient algorithms or resource-intensive operations in your code that contribute to higher energy consumption. By suggesting more efficient alternatives, it can help reduce the environmental impact of your software. |
| 20 | + |
| 21 | +## Example scenario |
| 22 | + |
| 23 | +The following Python code reads a large text file and counts the number of lines. However, it loads the entire file into memory, which can be inefficient for large files and lead to higher energy consumption. It also manually counts the lines instead of using built-in functions. |
| 24 | + |
| 25 | +```python id=inefficient-code |
| 26 | +def count_lines(filename): |
| 27 | + with open(filename, 'r') as f: |
| 28 | + data = f.read() |
| 29 | + lines = data.split('\n') |
| 30 | + count = 0 |
| 31 | + for line in lines: |
| 32 | + count += 1 |
| 33 | + return count |
| 34 | + |
| 35 | +print(count_lines('largefile.txt')) |
| 36 | +``` |
| 37 | + |
| 38 | +## Example prompt |
| 39 | + |
| 40 | +Here is an example prompt you can use with {% data variables.copilot.copilot_chat_short %} to refactor the above code for better environmental sustainability: |
| 41 | + |
| 42 | +```copilot copy prompt ref=inefficient-code |
| 43 | +Refactor this code to improve its environmental sustainability by reducing memory usage and computational overhead. |
| 44 | +``` |
| 45 | + |
| 46 | +## Example response |
| 47 | + |
| 48 | +> [!NOTE] {% data variables.copilot.copilot_chat_short %} responses are non-deterministic, so you may get a different response from the one shown here. |
| 49 | +
|
| 50 | +{% data variables.product.prodname_copilot_short %} suggests using a generator expression to read the file line by line, which reduces memory usage. It also uses the built-in `sum` function to count the lines more efficiently. |
| 51 | + |
| 52 | +```python |
| 53 | +def count_lines(filename): |
| 54 | + with open(filename, 'r') as f: |
| 55 | + return sum(1 for _ in f) # Efficiently counts lines without loading all into memory |
| 56 | + |
| 57 | +print(count_lines('largefile.txt')) |
| 58 | +``` |
| 59 | + |
| 60 | +## Further reading |
| 61 | + |
| 62 | +{% data reusables.copilot.example-prompts.further-reading-items %} |
0 commit comments