The rate at which energy is consumed. Analogy: Car speed (90 km/h).
The total amount used over time.
Energy = Power × Time
Analogy: Distance traveled (200 km).
Our Goal: Minimize the total Joules for a specific task.
Running Average Power Limit
Modern CPUs don't have physical meters inside, but they have accurate Power Models.
Linux exposes RAPL via /sys/class/powercap/intel-rapl/. Let's build a Python decorator:
import time
def read_uj(): return int(open("/sys/class/powercap/intel-rapl:0/energy_uj").read())
def measure_energy(f):
def wrapper(*args, **kwargs):
e_start, t_start = read_uj(), time.time()
res = f(*args, **kwargs)
joules = (read_uj() - e_start) / 1e6
print(f"[{f.__name__}] {joules:.2f}J in {time.time()-t_start:.2f}s")
return res
return wrapper
@measure_energy
def process_data(records):
# Heavy computation here
return sorted(records, key=lambda x: x['value'])
# Output:
# [process_data] 12.45 Joules in 0.85s
Perfect for local benchmarking of specific functions!
| Tool | Best For... | Command |
|---|---|---|
| powertop | System-wide diagnosis & "vampire" processes. | sudo powertop |
| powerstat | Real-time monitoring of system drain. | sudo powerstat |
| perf | Precision surgical measurement of a command. | sudo perf stat -e power/energy-pkg/ |
Let's create a 1GB text file:
$ tr -dc 'A-Za-z0-9 ' < /dev/urandom | fold -w 80 | head -c 1G > big.txt
# Single-threaded
$ sudo perf stat -e power/energy-pkg/ -- sort --parallel=1 big.txt > /dev/null
# 25s @ ~18W = 455 Joules
# 16 Threads
$ sudo perf stat -e power/energy-pkg/ -- sort --parallel=16 big.txt > /dev/null
# 6.4s @ ~24W = 151 Joules

Calculate the 1,000,000th prime number, using the same algorithm.
def is_prime(n):
if n <= 1: return False
if n <= 3: return True
if n % 2 == 0 or n % 3 == 0: return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
fn is_prime(n: u64) -> bool {
if n <= 1 { return false; }
if n <= 3 { return true; }
if n % 2 == 0 || n % 3 == 0 { return false; }
let mut i = 5;
while i * i <= n {
if n % i == 0 || n % (i + 2) == 0 {
return false;
}
i += 6;
}
true
}
| Language | Nr | Time | Energy (J) |
|---|---|---|---|
| Python |
15485863 | 50.2 s | 934 J |
| Rust |
15485863 | 2.1 s | 39 J |
Rust is 23x more energy-efficient.
Why?
Standard cloud runners (VMs) lack power meters. Instead, track proxy metrics like CPU Instructions or Cycles.
# .github/workflows/energy-test.yml
steps:
- run: cargo build --release
- name: Measure Proxy Metrics
run: |
sudo perf stat -e instructions -x, -o perf.csv \
./target/release/my_app --benchmark
INSTR=$(awk -F, '/instructions/ {print $1}' perf.csv)
# Fail build if instruction count spikes
if (( $(echo "$INSTR > 500000000" | bc -l) )); then
echo "Efficiency regression! $INSTR instructions used."
exit 1
fi
On the cloud, hypervisors hide RAPL. How do we measure containers?
Shift your focus to "Joules per Request".
Why efficiency isn't enough.
As technological progress increases efficiency, the rate of consumption rises due to increasing demand.
powercap.perf stat in your CI pipeline.@ilmanzoQuestions?