help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Check if the environment is Octave or Matlab


From: Colin Macdonald
Subject: Re: Check if the environment is Octave or Matlab
Date: Fri, 10 Apr 2020 13:18:25 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0

On 2020-04-10 12:21 p.m., Mike Miller wrote:
As the manual shows, if you are assigning a variable anyway, avoid the
if-else:

     octave = (exist('OCTAVE_VERSION', 'builtin') == 5);

and as others have suggested, you could make that persistent and/or
global, depending on your needs.

We did some tests in the doctest project and decided that caching in a persistent variable wasn't worth it. Here's our function, which has some timing info in the docstring:


function r = is_octave()
%IS_OCTAVE  True if we are running Octave, false for Matlab.

%%
% Copyright (c) 2015 Colin B. Macdonald
% SPDX-License-Identifier: BSD-3-Clause


% Timings for different implementations, 10000 calls
%
%     test        Matlab    Octave
%     ----------------------------
%     try-catch   1.2s      0.18s
%     if-exist    0.14s     0.22s
%     dummy       0.13s     0.13s
%
% Conclusions: "if-exist" only twice as slow as "dummy" (always return
% true), so no need to bother with a persistent variable.

  r = exist('OCTAVE_VERSION', 'builtin') ~= 0;

  %try
  %  OCTAVE_VERSION;
  %  r = true;
  %catch
  %  r = false;
  %end

end




reply via email to

[Prev in Thread] Current Thread [Next in Thread]