High-performance blocking timer

版本 1.0.2.0 (35.3 KB) 作者: Bradley Treeby
High-performance timing functions for MATLAB (Windows only)
27.0 次下载
更新时间 2023/3/16

Matlab Blocking Timer

View High-performance blocking timer on File Exchange

Overview

The Matlab Blocking Timer provides two functions, getTime and waitUntilTime which can be used for performance critical timing operations in MATLAB running under Windows. These provide high-performance timing with jitter / drift on the order of ~1 ms. The functionality was inspired by the GetSecs and WaitSecs('UntilTime') functions in the Psychtoolbox which offer similar performance, but require the Psychtoolbox.

Installation

The timer functions can be installed directly from the MATLAB Add-On Explorer. Select Add-Ons and search for Blocking Timer, then select Add, Add to MATLAB.

Usage

The functions can be used to perform a task at a specified interval. To avoid drift between successive events, it is recommended to reference the wait times against a static start time.

% set number of iterations and repetition interval in seconds
numIterations = 1000;
repetitionInterval = 0.5;

% get start time, and start loop
startTime = getTime();
for ind = 1:numIterations

    % wait until specified time
    waitUntilTime(startTime + ind * repetitionInterval);

    % do something...

end

The getTime funciton can also be used as an alternative to tic / toc to implement precision timing.

startTime = getTime;

% do something...

disp(['Elapsed time is ' num2str(getTime - startTime) ' seconds.']);

⚠️ waitUntilTime is a blocking timer. This means MATLAB will be unresponsive until the specified time is reached!

Performance

Two test scripts are provided in the examples folder. The testTimerJitter script checks for the timing-jitter using 500 successive calls to waitUntilTime. The testTimerDrift script checks for drift between 500 successive events, where the desired time between events is 500 ms. Run under real-world conditions, the timing accuracy is around ~1 ms (see histograms below).

Implementation

The functions use the C++ MEX API to implement the timing functions. The current time is calculated using the Windows QueryPerformanceCounter, which is a high resolution (<1us) time stamp. The blocking timer is implemented using the Windows multimedia timer API. This combination gave the best performance compared to using std::chrono::high_resolution_clock::now() to get the time, and std::this_thread::sleep_for, Sleep, or CreateWaitableTimer to implement the blocking timer.

Compiling

Compiled versions of the getTime.cpp and waitUntilTime.cpp mex functions are included in the repository. These were compiled using Windows 10 with MATLAB 2022b and the Microsoft Visual C++ 2022 compiler.

To compile the codes, call:

mex -lkernel32 -lwinmm -largeArrayDims waitUntilTime.cpp
mex -lkernel32 -largeArrayDims getTime.cpp

引用格式

Bradley Treeby (2024). High-performance blocking timer (https://github.com/btreeby/matlab-blocking-timer/releases/tag/v1.0.2), GitHub. 检索时间: .

MATLAB 版本兼容性
创建方式 R2022b
兼容任何版本
平台兼容性
Windows macOS Linux
标签 添加标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.2.0

See release notes for this release on GitHub: https://github.com/btreeby/matlab-blocking-timer/releases/tag/v1.0.2

1.0.1.0

See release notes for this release on GitHub: https://github.com/btreeby/matlab-blocking-timer/releases/tag/v1.0.1

1.0.0

要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库
要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库