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.
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.
Performance results
Using JMH (Java Microbenchmark Harness):
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
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:
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.