Performance Optimization with a Sustainability Perspective (Java)
Optimized code runs faster and consumes less energy — showing how performance improvements can also reduce estimated carbon emissions.

Performance Optimization with a Sustainability Perspective (Java)

Performance optimisation is often discussed in terms of speed and scalability. Increasingly, it also has a sustainability impact that engineers should not ignore.

Recently, I refactored a Java method and compared two implementations that solve the same problem: enriching active projects with their current task names.

Article content
Java Code - Traditional way of loop and if statements

This version works correctly, but it relies on nested loops and repeated comparisons, which increases CPU work as data grows.

Using lambda expressions and stream operations, the intent of the code becomes clearer, and in this case, the JVM is able to optimize execution more effectively.
Java code - More expressive Lambda style.

Using lambda expressions and stream operations, the intent of the code becomes clearer, and in this case, the JVM is able to optimize execution more effectively.

Performance results

Using JMH (Java Microbenchmark Harness):

  • The lambda-based implementation showed a ~25 ms reduction in execution time per run under the same workload.

This improvement reduces CPU active time, which directly affects energy consumption.


Carbon emission estimation (standards-aligned)

Carbon emissions are estimated, not directly measured in Java. The calculation follows widely accepted sustainability engineering practices.

Assumptions

  • Execution time improvement: 25 ms = 0.025 s
  • Average CPU power under load: 30 W
  • Grid carbon intensity: 700 gCO₂ / kWh (example regional average)

Calculation

Energy saved per execution

Energy = Power × Time
Energy = 30 W × 0.025 s = 0.75 J        

Convert Joules to kWh

0.75 / 3,600,000 ≈ 2.08 × 10⁻⁷ kWh        

Carbon saved per execution

CO₂ = Energy × Carbon Intensity
CO₂ ≈ 0.00015 g CO₂        

Daily impact at scale

This function is called approximately 6,250 times per day.

Daily CO₂ reduction ≈ 0.00015 × 6,250
≈ 0.94 g CO₂ per day        

While the per-call saving is small, the cumulative effect becomes meaningful as systems scale and traffic grows.


Key takeaway

Lambda expressions are often used to produce cleaner, more expressive code. This example shows they can also:

  • improve execution performance,
  • reduce CPU energy usage, and
  • contribute to a lower estimated carbon footprint.

Efficient code is not just faster code — it is more sustainable code.

Saravanan Kuppusamy Splendid and when done over time across multiple code repositories, it will deliver a meaningful outcome.

Like
Reply

To view or add a comment, sign in

More articles by Saravanan Kuppusamy

Explore content categories