Measure inference time + throughput
How fast can the model make predictions? Measure inference time, per-sample latency, and throughput (samples/sec).
script.py
v3.1
Python output
click Run to execute
Click "Run" to execute the code on the left.
The output will appear here.
The output will appear here.
🧠 How it works
Three metrics matter for inference: (1) TOTAL inference time — how long for the whole test set. (2) PER-SAMPLE LATENCY — inference_time / n_samples. What users feel. (3) THROUGHPUT — n_samples / inference_time. How many requests/sec the model can handle. WARM-UP is critical: the first predict() call is slower (lazy initialization, JIT compilation). Always warm up before timing, or your numbers will be skewed. For real-time APIs, measure SINGLE-SAMPLE latency (predict 1 sample, average over 100 calls). Batch prediction is always faster per-sample (amortized overhead).
✏️ Try these changes
-
Change X_test size from 1000 to 10000→ bigger batch → higher throughput -
Change n_estimators from 50 to 200→ RandomForest takes longer per prediction -
Remove the warm-up call→ first prediction will be slower, skewing the average -
Change the single-sample loop from 100 to 1000 iterations→ more stable average