Green AI – Programming Language Impact on Energy Consumption
Study Aim
- Investigate impact of programming languages on AI energy efficiency.
- Main RQ: "How do programming languages affect AI energy consumption during training and inference?"
Experimental Design
- Programming languages: 5 (C++, Java, Python, MATLAB, R).
- Algorithms: 7 (KNN, SVC, AdaBoost, Decision Tree, Logistic Regression, Naive Bayes, Random Forest).
- Datasets: 3 UCI sets (Iris 150 instances 4 features, Breast Cancer 699 instances 30 features, Wine Quality 4.9k instances 4 features).
- Phases analyzed: Training vs. Inference (data split 80% train, 20% test).
- Runs: 30 repetitions per configuration ⇒ 6.3k total executions.
- Hardware: Apple M2, 8-core CPU, 10-core GPU, 8 GB RAM.
- Energy metric: Total Joules (CPU + GPU + RAM) via CodeCarbon.
Key Training Results (RQ1.1)
- Energy ranking (least to most):
- C++
- Java (≈ 4× C++)
- MATLAB (≈ 7× C++)
- Python (≈ 15× C++)
- R (≈ 37× C++)
- Outliers heavily influence totals:
• R Logistic Regression = 71.1% of R’s training energy.
• Python SVC = 63.38% of Python’s training energy. - Java supplies most energy-efficient implementations for 4/7 algorithms; C++ lowest cumulative energy.
Key Inference Results (RQ1.2)
- Energy ranking (least to most):
- Java
- C++ (≈ 2× Java)
- Python (≈ 35× Java)
- R (≈ 39× Java)
- MATLAB (≈ 54× Java)
- Single-algorithm impacts:
• Python SVC = 64.8% Python inference energy.
• MATLAB Naive Bayes = 62.7% MATLAB inference energy. - Java hosts 4/7 most efficient inference algorithms; C++ hosts remaining 3.
Core Insights
- Compiled / semi-compiled languages (C++, Java) markedly greener than interpreted ones (Python, MATLAB, R) — up to 54× difference.
- Efficiency ranking flips between phases; context (training vs. inference) matters.
- No algorithm is intrinsically energy-greedy across all languages; implementation quality dominates.
- Algorithm implementation can override language effects; e.g., C++ Decision Tree is 2nd worst despite overall C++ efficiency.
Practical Recommendations
- Choose language by dominant phase:
• C++/Java for sustained training or inference at scale.
• MATLAB suitable for prototyping; avoid for large-scale inference. - Before refactoring, profile specific library implementation; swapping algorithm library may yield bigger gains than switching language.
- Balance energy gains against development & maintenance cost; pre-compiled libraries in high-level languages can mitigate overhead.
Threats to Validity (summary)
- Limited datasets, algorithms, and single library per language; results not universal.
- Measurements obtained on single M2 machine; other hardware may differ.
- Energy only, excludes carbon-intensity variability.
Future Work
- Extend to more datasets, deep-learning workloads, additional libraries and hardware.
- Compare multiple implementations within the same language.
- Explore optimisation techniques that raise energy efficiency without abandoning current development stacks.
- Experiment emitted ≈ 42.9 g CO2 (≈ 0.39 km electric-car travel).