Performance Profiler
"Is OnTick too slow" is usually answered by timing the whole function with a single tick-count pair, which gives a total and nothing else. If OnTick reads five indicators, loops the open positions and writes to a file, that one number cannot say which of those is actually the expensive part - and the expensive part is rarely the one that looks expensive in the source. CProfiler lets you wrap as many named regions as you want: Start("Indicators")/Stop("Indicators"), Start("OrderScan")/Stop("OrderScan"), and so on, and reports each one's call count, total, average, minimum and maximum time independently, in milliseconds. Timing uses GetMicrosecondCount(), the highest-resolution clock the platform exposes, so even a region that takes a fraction of a millisecond produces a meaningful reading. Region storage is a fixed-size table, not a dynamic array. Calling ArrayResize() inside OnTick every time a region is touched would add allocation overhead that can rival or exceed the microseconds being measured - a profiler that noticeably slows down the code it is profiling has defeated its own purpose. PROFILER_MAX_REGIONS caps the table at 32 distinct region names per CProfiler instance; a region requested beyond that is refused with a warning rather than silently dropped or grown. Mismatched Start()/Stop() pairs are the most common misuse of any timer, so both directions are handled explicitly rather than left to crash or misbe- have: a Stop() with no matching Start() is reported and ignored, and a second Start() before the matching Stop() restarts the timer and discards the abandoned interval, with a warning either way. Report() also flags any region still running when it is called - a leaked Start() with no Stop() is exactly the bug that flag exists to surface, since that region's numbers are meaningless until it is closed. Notes and limitations - Not reentrant on the same region name - starting "X" from two different call sites before either stops it is exactly the double-Start case, and the first interval is discarded, not merged. Use two different region names for two different call sites even if they measure similar work. - GetMicrosecondCount() measures wall-clock time on the terminal thread, not CPU time - a region that yields (e.g. touches something that blocks) will show inflated numbers that are not purely computation. - The 32-region cap is per CProfiler instance, not global - declare more than one instance if a single project genuinely needs more than 32 distinct named regions, though that is usually a sign the regions are too finely sliced to read at a glance anyway. - This measures time only. It does not measure memory - see MemoryReport, a separate submission, for that.
Script · MT4 · v1.0 · Free
About Performance Profiler
Performance Profiler is a script for MetaTrader 4. It has 1 setting, such as Iterations. MQL source code included.
Files in the download: Profiler.mqh, Profiler.txt, ProfilerDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Iterations | 200 | How many times to time each region |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code