tic — Start a high-resolution stopwatch and optionally return a handle for toc.
tic starts a high-resolution stopwatch. Calls to toc report the elapsed time in seconds. When you assign the return value (for example, t = tic;), the resulting handle can be passed to toc(t) to measure a different code region while keeping the global stopwatch untouched.
How tic works in RunMat
- Uses the host's monotonic clock for nanosecond-resolution timing.
- Supports nested timers: each call pushes a new start time on an internal stack.
tocwithout inputs always reads the most recentticand removes it, leaving earlier timers intact so outer scopes continue measuring. - Returns an opaque scalar handle (a
double) that encodes the monotonic timestamp. The handle can be stored or passed explicitly totoc. - Executes entirely on the CPU. There are no GPU variants because
ticinteracts with wall-clock state. - Calling
tocbeforeticraises the MATLAB-compatible errorRunMat:toc:NoMatchingTic.
Examples
Measuring a simple loop
tic;
for k = 1:1e5
sqrt(k);
end
elapsed = tocCapturing and reusing the tic handle
t = tic;
heavyComputation();
elapsed = toc(t)Nesting timers for staged profiling
tic; % Outer stopwatch
stage1(); % Work you want to measure once
inner = tic; % Nested stopwatch
stage2();
innerT = toc(inner); % Elapsed time for stage2 only
outerT = toc; % Elapsed time for everything since the first ticMeasuring asynchronous work
token = tic;
future = backgroundTask();
wait(future);
elapsed = toc(token)Resetting the stopwatch after a measurement
elapsed1 = toc(tic); % Equivalent to separate tic/toc calls
pause(0.1);
elapsed2 = toc(tic); % Starts a new timer immediatelyFAQ
Does tic print anything when called without a semicolon?
No. tic is marked as a sink builtin, so scripts do not display the returned handle unless you assign it or explicitly request output.
Is the returned handle portable across sessions?
No. The handle encodes a monotonic timestamp that is only meaningful within the current RunMat process. Passing it to another session or saving it to disk is undefined behaviour, matching MATLAB.
Can I run tic on a worker thread?
Yes. Each thread shares the same stopwatch stack. Nested tic/toc pairs remain well-defined, but you should serialise access at the script level to avoid interleaving unrelated timings.
How accurate is the measurement?
tic relies on a monotonic clock (via runmat_time::Instant), typically providing microsecond or better precision. The actual resolution depends on your operating system. There is no artificial jitter or throttling introduced by RunMat.
Does tic participate in GPU fusion?
No. Timer builtins are tagged as CPU-only. Expressions containing tic are always executed on the host, and any GPU-resident tensors are gathered automatically by surrounding code when necessary.
Related functions to explore
These functions work well alongside tic. Each page has runnable examples you can try in the browser.
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how tic works, line by line, in Rust.
- View tic.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.