Loading…

Benchmark Harness

"I rewrote this loop and it's faster now" is a claim, not a measurement. This library is the measurement: define each competing implementation as a small class with a Run() method, hand an array of them to BenchmarkCompare, and get back a ranked table with mean, standard deviation, min and max time per call, and a ratio against the fastest entry. Neither MQL4 nor MQL5 has function pointers, so a "case" cannot be passed around as a bare function the way it could in a language that has them. This uses the ask-tell interface pattern instead: CBenchmarkCase is an abstract base class with a pure virtual Run(), each implementation under test subclasses it, and the harness is handed an array of base-class pointers - virtual dispatch through the vtable does the job a function pointer would otherwise do, and it is the idiomatic MQL4/5 way to pass "behaviour" as data. Every repetition is timed individually with GetMicrosecondCount(), not inferred from dividing a single batch time by the repeat count. That matters because a single slow outlier - a terminal-side pause, a page fault, a background tick - would otherwise get averaged into every sample invisibly. Timing each call separately means that kind of noise shows up honestly in the standard deviation and the max column instead of silently lowering the mean's credibility. A warmup phase runs first and is discarded, so one-time setup costs and any initial-call overhead do not bleed into the measured average. Notes and limitations - Timings measure wall-clock time in the running terminal process, not CPU cycles - background load from other charts, indicators or the terminal itself shows up in the numbers. Close unrelated charts before trusting a close call between two implementations. - Run() must not have side effects that make later calls faster or slower than earlier ones (growing a cache, appending to a file) unless that effect is genuinely part of what is being measured - the harness assumes every repetition does equivalent work. - GetMicrosecondCount() resolution is finer than a millisecond but is not a hardware cycle counter; work that completes in a handful of microseconds will show more relative noise than work that takes tens of microseconds - increase reps for very fast operations rather than trusting a single run. - BenchmarkCompare's own sort is O(n^2), which is irrelevant for the handful of cases a benchmark run typically compares and was chosen over pulling in a second include for it.

Script · MT4 · v1.0 · Free

About Benchmark Harness

Benchmark Harness is a script for MetaTrader 4. MQL source code included.

Files in the download: BenchmarkHarness.mqh, BenchmarkHarness.txt, BenchmarkHarnessDemo.mq4.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria