Skip to main content

Refactoring for environmental sustainability

Copilot Chat can suggest ways to make code more environmentally friendly.

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.

Copilot Chat 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.

Example scenario

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.

def count_lines(filename):
    with open(filename, 'r') as f:
        data = f.read()
        lines = data.split('\n')
        count = 0
        for line in lines:
            count += 1
        return count

print(count_lines('largefile.txt'))

Example prompt

Here is an example prompt you can use with Copilot Chat to refactor the above code for better environmental sustainability:

Copilot Chat prompt
Refactor this code to improve its environmental sustainability by reducing memory usage and computational overhead.

Example response

Hinweis

Copilot Chat responses are non-deterministic, so you may get a different response from the one shown here.

Copilot 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.

def count_lines(filename):
    with open(filename, 'r') as f:
        return sum(1 for _ in f)  # Efficiently counts lines without loading all into memory

print(count_lines('largefile.txt'))

Further reading