Concepts 🚀 Project 🧮 Labs
🧮 Labs / Benchmark Engine / Measure training time (time.perf_counter)

Measure training time (time.perf_counter)

The 'benchmark' part: how long does model.fit() take? Use time.perf_counter() for high-precision timing.

script.py v3.1
Python output click Run to execute
Click "Run" to execute the code on the left.
The output will appear here.
🧠 How it works

time.perf_counter() is the right tool for measuring durations. Higher resolution than time.time() (which is for wall-clock time). Monotonic — never goes backwards (important on multi-core systems). The benchmark function wraps fit() with start/end timers. LinearRegression is fastest (closed-form solution). RandomForest is slowest (builds 50 trees). This is what 'benchmarking' means — measuring TIME so you can compare models.

✏️ Try these changes
  • Change n_samples from 10000 to 100000
    → 10x more data → all models take longer
  • Change n_estimators from 50 to 200
    → RandomForest takes 4x longer
  • Add from sklearn.svm import SVR; benchmark_training(SVR(), X[:1000], y[:1000], 'SVR')
    → SVMs are slow on large data
  • Wrap the timing in a loop (run fit 5 times, take average)
    → more stable measurement