title: Is MEX of MATLAB known to be slow on macOS?
body: <p>Question: <strong>Is MEX of MATLAB known to be slow on macOS?</strong></p>
  <p>Here, &quot;slow&quot; refers to the speed of setting MEX up, of compiling MEX code, and of running the MEX function.</p>
  <p>I have done some timing using <a href="https://github.com/equipez/test_matlab/actions/runs/1902217520" rel="nofollow noreferrer">GitHub Actions</a>. The hardware specification is as follows. It is copied from the <a href="https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners" rel="nofollow noreferrer">official documentation of GitHub Actions</a>. The detailed information of CPU (frequency etc) and memory are not available.</p>
  <p>GNU/Linux (ubuntu 20.04) and Windows (Windows Server 2019):</p>
  <ul>
  <li>2-core CPU</li>
  <li>7 GB of RAM memory</li>
  <li>14 GB of SSD disk space</li>
  </ul>
  <p>macOS (macOS Big Sur 11):</p>
  <ul>
  <li>3-core CPU</li>
  <li>14 GB of RAM memory</li>
  <li>14 GB of SSD disk space</li>
  </ul>
  <p>Here is the data obtained.</p>
  <pre><code>System: GNU/Linux | Language: C | MATLAB: 2021b | Time: 2022.02.26 05:46:36
  MEX configured to use 'gcc' for C language compilation.
  - Time for setting MEX up: 0.477950 seconds
  - Time for mexifying timestwo: 4.500026 seconds
  - Time for 100 runs of timestwo: 0.003845 seconds
  
  System: Windows | Language: C | MATLAB: 2021b | Time: 2022.02.26 05:47:56
  MEX configured to use 'Microsoft Visual C++ 2019 (C)' for C language compilation.
  - Time for setting MEX up: 2.518557 seconds
  - Time for mexifying timestwo: 4.416958 seconds
  - Time for 100 runs of timestwo: 0.004215 seconds
  
  System: macOS | Language: C | MATLAB: 2021b | Time: 2022.02.26 05:49:01
  MEX configured to use 'Xcode with Clang' for C language compilation.
  - Time for setting MEX up: 17.602277 seconds
  - Time for mexifying timestwo: 5.979585 seconds
  - Time for 100 runs of timestwo: 0.130843 seconds
  
  
  System: GNU/Linux | Language: Fortran | MATLAB: 2021b | Time: 2022.02.26 05:46:20
  MEX configured to use 'gfortran' for FORTRAN language compilation.
  - Time for setting MEX up: 0.835881 seconds
  - Time for mexifying timestwo: 2.768746 seconds
  - Time for 100 runs of timestwo: 0.003279 seconds
  
  System: Windows | Language: Fortran | MATLAB: 2021b | Time: 2022.02.26 05:51:04
  MEX configured to use 'Intel oneAPI 2021 for Fortran with Microsoft Visual Studio 2019' for FORTRAN language compilation.
  - Time for setting MEX up: 1.660305 seconds
  - Time for mexifying timestwo: 3.495534 seconds
  - Time for 100 runs of timestwo: 0.003299 seconds
  
  System: macOS | Language: Fortran | MATLAB: 2021b | Time: 2022.02.26 05:49:47
  MEX configured to use 'Intel Fortran Composer XE' for FORTRAN language compilation.
  - Time for setting MEX up: 248.263933 seconds
  - Time for mexifying timestwo: 87.093711 seconds
  - Time for 100 runs of timestwo: 0.078741 seconds
  </code></pre>
  <p>It turns out that <strong>MEX is much slower on macOS than on GNU/Linux or Windows</strong>: slow to set up, slow to mexify, and the MEX function is slow to run. In particular, <strong>it is about 300 times slower to set up MEX for Fortran on macOS than on Linux</strong>. However, note that the significant difference probably comes from the virtual environment of GitHub Actions. On local machines, the difference may not be that dramatic.</p>
  <p>If you are interested in the timing, below is the code I used for it. It is also <a href="https://github.com/equipez/test_matlab/tree/master/mex_time" rel="nofollow noreferrer">available on GitHub</a>.</p>
  <pre><code>function mex_time(language)
  % MEX_TIME measures the running time of MATLAB concerning MEX.
   
  orig_warning_state = warning;
  warning('off','all');
  
  if nargin == 1 &amp;&amp; (isa(language, 'char') || isa(language, 'string')) &amp;&amp; strcmpi(language, 'Fortran')
      language = 'Fortran';
      timestwo_src = 'timestwo.F';
  else
      language = 'C';
      timestwo_src = 'timestwo.c';
  end
  
  if ismac
      sys = 'macOS';
  elseif isunix
      sys = 'GNU/Linux';
  elseif ispc
      sys = 'Windows';
  else
      error('Platform not supported.')
  end
  
  matlab_version = version('-release');
  date_time = datestr(now,'yyyy.mm.dd HH:MM:SS');
  
  fprintf('\nSystem: %s | Language: %s | MATLAB: %s | Time: %s\n\n', sys, language, matlab_version, date_time);
  tic;
  
  mex('-setup', language);
  fprintf('\n- Time for setting MEX up: %f seconds\n\n', toc);
  
  clear('timestwo');
  tic;
  mex(fullfile(matlabroot, 'extern', 'examples', 'refbook', timestwo_src));
  fprintf('\n- Time for mexifying timestwo: %f seconds\n', toc);
  
  tic;
  for i = 1 : 100
      timestwo(i);
  end
  fprintf('\n- Time for 100 runs of timestwo: %f seconds\n\n', toc);
  delete('timestwo.*');
  
  warning(orig_warning_state);
  </code></pre>
  <p>You may try</p>
  <pre><code>mex_time('C');
  mex_time('Fortran'); 
  </code></pre>
  <p>on your computer and post the results here. Thank you very much.</p>
  <p>(See the discussions on <a href="https://www.mathworks.com/matlabcentral/answers/1658820-is-mex-known-to-be-slow-on-macos" rel="nofollow noreferrer">MATLAB Answers</a> and <a href="https://github.com/matlab-actions/setup-matlab/issues/30" rel="nofollow noreferrer">GitHub</a>)</p>