처리량과 지연 시간의 절충
초당 전체 작업량(처리량)과 항목당 소요 시간(지연 시간)의 균형을 맞춥니다. 항목별 처리, 일괄 처리, 병렬 처리 수준 조정을 비교합니다.
처리량과 지연 시간의 절충은(는) CoddyKit의 무료 C# Academy 강의입니다. 이것은 3개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 C# Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. C# Academy 강의에는 총 3개의 강의가 포함되어 있습니다.
처리량과 지연 시간
정의:
- 처리량: 초당 처리하는 항목 수
- 지연 시간: 하나의 항목을 완료하는 데 걸리는 시간
- 절충: 일괄 처리와 더 높은 병렬 처리는 처리량을 높일 수 있지만 개별 항목을 지연시킬 수 있습니다
항목별 처리 방식
도착하는 각 항목을 바로 처리합니다. 항목별 대기 시간은 최소화되지만 모든 항목에서 오버헤드가 반복됩니다.
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
// Simulate small per-item cost
static void HandleItem(int x)
{
// Fixed overhead per item
Thread.SpinWait(20000); // tiny CPU work
}
public static void Main(string[] args)
{
int n = 200;
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < n; i++)
{
HandleItem(i); // process immediately (no batching)
// emit result right away (low latency style)
}
sw.Stop();
Console.WriteLine("Per-item style: {0} ms for {1} items", sw.ElapsedMilliseconds, n);
}
}
일괄 처리 방식
일괄 처리는 반복되는 오버헤드를 줄이고 처리량을 높이지만, 먼저 도착한 항목은 묶음이 채워질 때까지 기다리므로 지연 시간이 늘어납니다.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
public class Program
{
static void ProcessBatch(List<int> batch)
{
// Amortize overhead across the whole batch
Thread.SpinWait(20000); // one-time overhead
for (int i = 0; i < batch.Count; i++)
{
// small per-record work
int val = batch[i] * 2;
if (val == int.MinValue) { } // keep compiler from dropping work
}
}
public static void Main(string[] args)
{
int n = 200;
int batchSize = 20;
List<int> current = new List<int>(batchSize);
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < n; i++)
{
current.Add(i);
if (current.Count == batchSize)
{
ProcessBatch(current);
current.Clear(); // emit results after the batch finishes
}
}
if (current.Count > 0) ProcessBatch(current);
sw.Stop();
Console.WriteLine("Batch style: {0} ms for {1} items (batch={2})", sw.ElapsedMilliseconds, n, batchSize);
}
}
병렬 처리 조정
MaxDegreeOfParallelism을 조정하면 CPU 중심 작업의 처리량을 높일 수 있지만, 너무 높이면 컨텍스트 전환 때문에 성능이 저하될 수 있습니다.
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
static void Work(int x)
{
// CPU-bound unit
Thread.SpinWait(40000);
}
public static void Main(string[] args)
{
int[] data = new int[200];
for (int i = 0; i < data.Length; i++) data[i] = i;
foreach (int dop in new int[] { 1, 2, 4 })
{
var opt = new ParallelOptions();
opt.MaxDegreeOfParallelism = dop;
Stopwatch sw = Stopwatch.StartNew();
Parallel.ForEach(data, opt, Work);
sw.Stop();
Console.WriteLine("DOP={0} -> {1} ms", dop, sw.ElapsedMilliseconds);
}
}
}
마이크로 배치 개념
마이크로 배치는 두 목표의 균형을 맞출 수 있습니다. 항목별 처리보다 처리량이 높고, 매우 큰 묶음보다 지연 시간이 짧습니다.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
static void ProcessBatch(List<int> batch)
{
Thread.SpinWait(15000); // small shared overhead
for (int i = 0; i < batch.Count; i++) Thread.SpinWait(2000);
}
public static void Main(string[] args)
{
int n = 200;
int micro = 5; // micro-batch size
List<int> buf = new List<int>(micro);
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < n; i++)
{
buf.Add(i);
if (buf.Count == micro)
{
ProcessBatch(buf);
buf.Clear(); // emit more frequently than big batches
}
}
if (buf.Count > 0) ProcessBatch(buf);
sw.Stop();
Console.WriteLine("Micro-batch (size={0}): {1} ms", micro, sw.ElapsedMilliseconds);
}
}
조정 팁
조정 지침:
- ms/항목과 항목/초를 모두 측정합니다
- 먼저 마이크로 배치를 시도합니다
- 병렬 처리를 천천히 늘리면서 CPU와 컨텍스트 전환을 확인합니다
- 긴 대기와 메모리 증가를 피하도록 queue에 용량 제한을 둡니다
처리량과 지연 시간의 절충
요약
요약: 항목별 처리는 지연 시간이 낮고 처리량이 낮습니다. 큰 묶음이나 높은 DOP는 처리량이 높고 지연 시간이 높습니다. 마이크로 배치와 신중한 DOP 조정으로 두 목표의 균형을 맞출 수 있습니다.
자주 묻는 질문
“처리량과 지연 시간의 절충” 강의는 무료인가요?
네 — “처리량과 지연 시간의 절충” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 C# Academy 강의 전체를 잠금 해제할 수 있습니다. C# Academy 강의에는 총 3개의 강의가 포함되어 있습니다.
“처리량과 지연 시간의 절충”에서 뭘 배우나요?
초당 전체 작업량(처리량)과 항목당 소요 시간(지연 시간)의 균형을 맞춥니다. 항목별 처리, 일괄 처리, 병렬 처리 수준 조정을 비교합니다. 브라우저에서 직접 실행하는 실습 코드로 C# Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
C# Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 C# Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 3개 중 3번째 강의입니다.
“처리량과 지연 시간의 절충” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 C# Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 C# Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Parallel.ForEach, PLINQ
- Channels를 사용한 생산자/소비자(개요)
- 처리량과 지연 시간의 절충