Pre-loading M/MEX files for timing optimisation

There’s no simple answer to this, only guidelines, and how slow “slow” is depends a lot on your hardware, e.g., amount of RAM and if you have a fast SSD or a slow disc drive, what operating system, what functionality you use… A warmup trial can still make sense for demanding paradigms.

E.g., initial loading of a mex file takes longer, and mex files with lots of library dependencies, e.g., Screen, will take longer. But then the slowness, e.g., for Screen will be mostly during startup / OpenWindow, as after openwindow most needed stuff will be in memory and linked/initialized. However, multi-media functions relying on GStreamer, like Screen(‘Openmovie / OpenVideocapture’) etc. will cause GStreamer to dynamically load and configure plugins, adding extra delay. But then, you won’t call openwindow or openmovie during the timing critical part of a trial…

On Linux, Priority(n) with n > 0 before the start of your actual trial loop will also trigger memory locking, a unique feature of Linux only, not supportable on Windows/macOS. Here the OS will keep any data/code from being paged out to disc until you voluntarily free that data, preventing other running applications from stealing valuable fast RAM. And other optimizations done… You can notice this if you measure the execution time of the Priority(1) call. It could take fractions of a second to seconds to complete when the system transitions Matlab/Octave from “regular app” to “get as much of the needed stuff into memory and keep it there”.

But it all depends on your task/os/hardware how much this matters, I guess if your study has some training trials at the beginning anyway to familiarize the subject, uses Priority, and a Linux system, that will be as good as it gets. There are further optimizations available for specific cases for texture preloading, flipping, movie playback from memory (preloading the movie) etc., useful on a case by case basis for demanding cases. This thing, e.g., The MPI CyberMotion Simulator - YouTube, also implemented some paradigms involving high performance playback of stereo high resolution video, where visual and audio stimulation was driven by Psychtoolbox, synchronized with robot motion controlled by a different computer etc., and on the ~2010-2012 ish hardware needed a lot of movie playback engine optimizations and special flags to make it happen, which are not the default for your typical “play a movie with PTB” use case.

So it depends, but choosing Linux as OS with recommended graphics hardware and drivers is one big step in the right direction for common cases…
-mario