GreenCodeAnalyzer is a static code analysis tool that identifies energy-inefficient patterns in Python code and suggests optimizations to improve energy consumption. By shifting energy efficiency concerns "left" (earlier) in the development process, developers can make more sustainable coding decisions from the start. This tool focuses on the context of data science in Python.
You can find a more detailed report of GreenCodeAnalyzer in this website.
- Visual Code Annotations: VS Code extension that provides visual feedback with highlighted energy smells
- Optimization Suggestions: Provides specific recommendations to make code more energy-efficient
- Multiple Rule Detection: Covers various energy-inefficient patterns common in data science and ML code
The extension currently detects these energy code smells:
| Rule | Description | Impact |
|---|---|---|
batch_matrix_multiplication |
Sequential matrix multiplications instead of batched operations | Missed hardware acceleration opportunities |
blocking_data_loader |
Prevent using data loading strategies that stall GPU execution | The GPU may remain idle while waiting for data |
broadcasting |
Inefficient tensor operations that could use broadcasting | Unnecessary memory allocations |
calculating_gradients |
Computing gradients when not needed for training | Unnecessary computation overhead |
chain_indexing |
Chained Pandas DataFrame indexing operations | Extra intermediate objects creation |
conditional_operations |
Element-wise conditional operations in loops | Inefficient branching and repeated calculations |
data_parallelization |
Usage of torch.nn.DataParallel detected | Less efficient than DistributedDataParallel |
element_wise_operations |
Element-wise operations in loops | Inefficient iteration instead of vectorized operations |
excessive_gpu_transfers |
Frequent CPU-GPU tensor transfers | High data movement overhead |
excessive_training |
Training loops without early stopping mechanisms | Wasted computation after model convergence |
filter_operations |
Manual filtering in loops instead of vectorized operations | Increased CPU workload |
ignoring_inplace_ops |
Operations that could use in-place variants | Unnecessary memory allocations |
inefficient_caching_of_common_arrays |
Recreating identical arrays inside loops | Redundant memory and CPU usage |
inefficient_data_loader_transfer |
DataLoader transfer to GPU without pin_memory | Increased data transfer time |
inefficient_df_joins |
Repeated merges or merges without DataFrame indexing | High memory usage and increased computation time |
inefficient_iterrows |
Inefficient row-by-row Pandas iterations | Python overhead for operations |
large_batch_size_causing_memory_swapping |
Batch sizes causing memory swapping | Excessive disk I/O and system slowdown |
recomputing_group_by |
Repetitive group by operations on the same data | Redundant computation and memory usage |
reduction_operations |
Manual reduction operations using loops | Missed vectorization opportunities |
redundant_model_refitting |
Redundant retraining of models with unchanged data | Wasteful recalculation |
You can install the GreenCodeAnalyzer extension directly from the VS Code Marketplace:
- Open VS Code
- Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or press
Ctrl+Shift+X - Search for "GreenCodeAnalyzer" and click on the install button.
Alternatively, you can install it from the VS Code Marketplace website.
Once installed, you can analyze your Python code for energy inefficiencies:
- Open a Python file in VS Code
- Use one of the following methods to run the analyzer:
- Press
Ctrl+Shift+Pto open the Command Palette, then type and select "GreenCodeAnalyzer: Run Analyzer" - Right-click in the editor and select "Run GreenCodeAnalyzer" from the context menu
- Press
The analysis results will appear as decorations in your code editor, highlighting potential energy inefficiencies with suggestions for improvement.
To clear the analysis markers:
- Press
Ctrl+Shift+Pand select "GreenCodeAnalyzer: Clear Gutters" - Or right-click and select "Clear GreenCodeAnalyzer Gutters"
Each detected code smell includes:
- A description of the energy inefficiency
- A specific recommendation for optimization
Python 3.10 or higher
- VS Code
- Required Python packages:
- pandas
- numpy
- scikit-learn
- torch
- tensorflow
-
Clone this repository:
git clone https://github.com/ianjoshi/green-code-analyzer.git cd green-code-analyzer -
Install required dependencies:
# Using conda conda env create -f environment.yml conda activate gsl_venv # Or using pip pip install -r requirements.txt
-
Compile the VS Code extension (from the extension directory):
cd GreenCodeAnalyzer npm install npm run compile
You can analyze Python files directly from the command line:
python main.pyBy default, the tool will analyze the code in the data/test.py file. You can also specify a custom file by replacing this path.
Running main.py will output detected code smells with their line numbers, descriptions, and suggested optimizations in the terminal.
Alternatively, you can use the VS Code extension for a more interactive experience:
-
Inside the editor, open
GreenCodeAnalyzer/src/extension.tsand pressF5or run the command Debug: Start Debugging from the Command Palette (Ctrl+Shift+P). -
When prompted to choose a debug environment, select "VS Code Extension Development".
-
Open the file you are working on in the new VS Code window that opens.
-
To run the static analysis, run the command GreenCodeAnalyzer: Run Analyzer from the Command Palette (
Ctrl+Shift+P). -
The tool will analyze your code and display results with:
- Colored gutter to indicate energy smells
- Detailed hover information with rule descriptions and optimization suggestions
To clear annotations, run the command GreenCodeAnalyzer: Clear Gutters from the Command Palette (Ctrl+Shift+P).
The tool works by:
- Parsing Python code into an Abstract Syntax Tree (AST)
- Applying predefined rules to identify inefficient patterns
- Generating appropriate warnings and suggestions
- Visualizing results in the editor (VS Code extension) or terminal output
We welcome contributions to GreenCodeAnalyzer! Please see our Contributing Guidelines for details on how to get started.
We are committed to fostering an open and welcoming environment. Please read our Code of Conduct to understand the expectations for participation in our community.