---
id: wiki-2026-0508-efficiency
title: Efficiency
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Performance Efficiency, Resource Efficiency, Cost Efficiency]
duplicate_of: none
source_trust_level: A
confidence_score: 0.88
verification_status: applied
tags: [performance, efficiency, optimization, sre, cost]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
  language: python
  framework: prometheus
---

# Efficiency

## 매 한 줄
> **"매 useful output / resource consumed — 매 latency, throughput, $cost, watt 매 dimension 별 측정"**. 매 1972 Knuth "premature optimization" warning 후 신중함이 default, 매 2026 cloud cost + carbon footprint + energy efficiency 가 first-class metric.

## 매 핵심

### 매 Efficiency dimensions
- **Time**: latency p50/p95/p99, throughput RPS.
- **Space**: memory RSS, disk IOPS, network bytes.
- **Money**: $/request, $/MAU.
- **Energy**: watt/op, gCO2eq/request.

### 매 측정 → 개선 cycle
1. **Profile**: hotspot 의 identify (flamegraph).
2. **Hypothesize**: bottleneck 의 type (CPU? IO? Lock?).
3. **Optimize**: targeted change.
4. **Verify**: A/B with baseline, metric 의 statistical sig.

### 매 응용
1. API: cold-start 의 reduce.
2. ML inference: quantization, batching, KV cache.
3. CI: cache hit rate 의 maximize.

## 💻 패턴

### Latency histogram (Prometheus)
```python
from prometheus_client import Histogram
LATENCY = Histogram('http_request_duration_seconds', 'HTTP latency',
                    buckets=[0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5])

@LATENCY.time()
def handle(req): ...
```

### Cost-per-request rollup
```sql
-- BigQuery
SELECT
  service,
  SUM(billable_seconds * cpu_cost_per_sec) AS compute_usd,
  COUNT(*) AS requests,
  SUM(billable_seconds * cpu_cost_per_sec) / COUNT(*) AS usd_per_req
FROM service_metrics
WHERE _PARTITIONDATE = CURRENT_DATE() - 1
GROUP BY service
ORDER BY usd_per_req DESC;
```

### CPU flamegraph (py-spy)
```bash
# 매 production-safe sampling profiler
py-spy record -o flame.svg -d 60 -p $(pgrep -f gunicorn)
py-spy top -p $(pgrep -f gunicorn)
```

### Memory profiling (memray)
```bash
memray run --live ./app.py
memray flamegraph output.bin -o memflame.html
```

### Async IO efficiency
```python
import asyncio, httpx

# BAD — sequential
async def fetch_seq(urls):
    async with httpx.AsyncClient() as c:
        return [await c.get(u) for u in urls]

# GOOD — concurrent
async def fetch_par(urls):
    async with httpx.AsyncClient() as c:
        return await asyncio.gather(*[c.get(u) for u in urls])
```

### Carbon-aware scheduling
```python
import httpx
async def carbon_intensity(region: str) -> float:
    r = await httpx.AsyncClient().get(
        f"https://api.electricitymaps.com/v3/carbon-intensity/latest?zone={region}",
        headers={"auth-token": "TOKEN"})
    return r.json()["carbonIntensity"]  # gCO2eq/kWh

# 매 batch job 매 low-carbon window 의 schedule
async def maybe_run(region):
    ci = await carbon_intensity(region)
    if ci < 200: await run_batch()
    else: await asyncio.sleep(900)
```

### LLM inference batching
```python
from vllm import LLM, SamplingParams
llm = LLM(model="meta-llama/Llama-3.3-70B-Instruct",
          tensor_parallel_size=4,
          max_num_seqs=256)  # 매 batch 의 throughput
sp = SamplingParams(max_tokens=256)
outs = llm.generate(prompts, sp)  # 매 single forward pass 로 batch
```

### Cache efficiency dashboard
```promql
# Prometheus query — cache hit ratio
sum(rate(cache_hits_total[5m])) /
(sum(rate(cache_hits_total[5m])) + sum(rate(cache_misses_total[5m])))
```

## 매 결정 기준
| 상황 | Approach |
|---|---|
| Latency-critical (real-time) | tail-latency optimize, drop p99 outliers |
| Throughput (batch) | parallelism, vectorize |
| Cost 제약 | spot instance, autoscale, cache |
| Green ops | carbon-aware scheduling, region selection |

**기본값**: 매 profile-first, 매 measure both before/after, 매 single-dimension fixation 매 X.

## 🔗 Graph
- 부모: [[Site Reliability Engineering]]
- 변형: [[Latency]]
- 응용: [[Flame_Graphs]] · [[Profiling]]
- Adjacent: [[FinOps]] · [[Green Software]]

## 🤖 LLM 활용
**언제**: profile output → bottleneck classification, optimization 후보 ranking.
**언제 X**: 매 micro-benchmark 매 LLM 의 single-shot 평가 X — 매 측정 우선.

## ❌ 안티패턴
- **Premature optimization**: profile 없이 optimize.
- **Single-metric obsession**: latency 만 보고 cost 폭증.
- **Synthetic benchmark**: production traffic shape 무시.
- **No baseline**: 매 before 측정 없이 "fast" 주장.

## 🧪 검증 / 중복
- Verified (SRE Workbook ch.4, AWS Well-Architected Performance pillar).
- 신뢰도 A.

## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — efficiency 4 dimension + measurement loop |
