Search code examples
scilab

Measuring CPU time using Scilab


I'm new to working with Scilab, and I'm trying to run the code below, but when I try it keeps showing me this error::

test3(1000)                    //Line that I type to run the code
 !--error 4                   //First error
Undefined variable: cputime
at line       2 of function test3 called by:

I ran it using MATLAB, and it worked, but I can't figure out how to make it run using Scilab.

For sample code when typed using the Scilab editor, see below.

function test3(n)
t = cputime;
for (j = 1:n)
    x(j) = sin(j);
end
disp(cputime - t);

Solution

  • Typing help cputime in the Scilab console will reveal that this is not a Scilab function. The near-equivalent Scilab function is timer(), but its behavior is a bit different:

    • cputime in Matlab measures time since Matlab started
    • timer() measures time since the last call to timer()

    Here is your function rewritten in Scilab:

    function test3(n)
        timer()
        for j = 1:n
            x(j) = sin(j)
        end
        disp(timer())
    endfunction
    

    Note that Scilab functions must end with endfunction, and that semicolons are optional: line-by-line output is suppressed in Scilab by default.

    For completeness, I'll mention tic() and toc(), which work just like Matlab's tic and toc, measuring real-world time of computation.