Octave compared to Matlab

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Octave compared to Matlab

Peter Waller-2
I have used MatLab for some years now and it has been a challenge to avoid
loops.
eg.
for k=1:Nbr
  k(1,k)=testfunction(k);
end;

If this loop is run in matlab then 'testfunction' is compiled for every
'k'.
>From the octave manual I have understood that octave compiles every
function before executing it.
Does this mean that 'testfunction' is only compiled once during the loop?

How does octave compile, is the code optimized?


I would like to thank Mario Storti for his answer on my vector problem, it
is great to have an email list like this!

/Peter Waller


Reply | Threaded
Open this post in threaded view
|

Re: Octave compared to Matlab

Mario Storti-4

Here is what Octave does when you call a function file

(From the Octave manual: File: octave.info,  Node: Function Files)
>     When Octave defines a function from a function file, it saves the
> full name of the file it read and the time stamp on the file.  After
> that, it checks the time stamp on the file every time it needs the
> function.  If the time stamp indicates that the file has changed since
> the last time it was read, Octave reads it again.

So that the function is compiled only once. Even more you can avoid the
time spent in checking the time stamp of the file with the
configuration variable `ignore_function_time_stamp'.

(From the Octave manual: File: octave.info,  Node: Function Files)
>    If you know that your own function files will not change while you
> are running Octave, you can improve performance by setting the variable
> `ignore_function_time_stamp' to `"all"', so that Octave will ignore the
> time stamps for all function files.  Setting it to `"system"' gives the
> default behavior.  If you set it to anything else, Octave will check
> the time stamps on all function files.

I never used it. I think that it should be used with care. Perhaps
inside some construct try-catch-end_try_catch (see the command try).

Regards

Mario

%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%
Mario Alberto Storti                           | Fax: (54)(42) 55.09.44 |
Centro Internacional de Metodos Computacionales| Tel: (54)(42) 55.91.75 |
  en Ingenieria - CIMEC (INTEC/CONICET-UNL)    |........................|  
INTEC, Guemes 3450 - 3000 Santa Fe, Argentina                           |
Reply: [hidden email], http://venus.unl.edu.ar/gtm-eng.html |


>>>>> On Fri, 14 Aug 1998 10:49:30 +0200 (MET DST),
>>>>>      Peter Waller <[hidden email]> said:

> I have used MatLab for some years now and it has been a challenge to avoid
> loops.
> eg.
> for k=1:Nbr
>   k(1,k)=testfunction(k);
> end;

> If this loop is run in matlab then 'testfunction' is compiled for every
> 'k'.
>> From the octave manual I have understood that octave compiles every
> function before executing it.
> Does this mean that 'testfunction' is only compiled once during the loop?

> How does octave compile, is the code optimized?

> I would like to thank Mario Storti for his answer on my vector problem, it
> is great to have an email list like this!

> /Peter Waller


Reply | Threaded
Open this post in threaded view
|

Re: Octave compared to Matlab

Peter Waller-2


On Fri, 14 Aug 1998, Mario Storti wrote:

>
> So that the function is compiled only once. Even more you can avoid the
> time spent in checking the time stamp of the file with the
> configuration variable `ignore_function_time_stamp'.
>

Is it then true, that to make a calculation with loops takes
about the same time as with matrix formulation of the same problem?

/Peter Waller


Reply | Threaded
Open this post in threaded view
|

Re: Octave compared to Matlab

Mario Storti-4

>>>>> On Fri, 14 Aug 1998 16:16:29 +0200 (MET DST),
>>>>>      Peter Waller <[hidden email]> said:

> On Fri, 14 Aug 1998, Mario Storti wrote:

>>
>> So that the function is compiled only once. Even more you can avoid the
>> time spent in checking the time stamp of the file with the
>> configuration variable `ignore_function_time_stamp'.
>>

> Is it then true, that to make a calculation with loops takes
> about the same time as with matrix formulation of the same problem?

> /Peter Waller

No.  Execution of loops in Octave are intrinsically inefficient (as
compared with Fortran or C), even if you don't call functions inside
the loop. For instance with the 'cross.m' function I sent to you
yesterday, the vector form

> octave> tic ; c=cross(a,b); toc
> ans = 0

is far much faster than the loop

> octave> tic; for k=1:n;   c(k,1)=a(k,2).*b(k,3)-a(k,3).*b(k,2); \
    c(k,2)=a(k,3).*b(k,1)-a(k,1).*b(k,3);  \
    c(k,3)=a(k,1).*b(k,2)-a(k,2).*b(k,1); endfor; toc
> ans = 17

But the loop doesn't change very much if calling a function inside.

> octave> ignore_function_time_stamp="all";
> octave> tic; for k=1:n;   c(k,:)=cross(a(k,:),b(k,:)); endfor; toc
> ans = 21
> octave> ignore_function_time_stamp="system";
> octave> tic; for k=1:n;   c(k,:)=cross(a(k,:),b(k,:)); endfor; toc
> ans = 21

BTW, apparently, time stamp checking is not too much comsuming in this
case.

I tried with the `cross.m' that cames with octave too and it gave me an
elapsed time of 46. This is because it does a lot of error checking.

(I run all the examples in a Linux/Pentium 133Mhz, Octave 2.0.13. I
used n=3000. I used tic/toc but there were no interferences from other
processes on the system.)

Regards,

Mario

%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%%%%%%<>%
Mario Alberto Storti                           | Fax: (54)(42) 55.09.44 |
Centro Internacional de Metodos Computacionales| Tel: (54)(42) 55.91.75 |
  en Ingenieria - CIMEC (INTEC/CONICET-UNL)    |........................|  
INTEC, Guemes 3450 - 3000 Santa Fe, Argentina                           |
Reply: [hidden email], http://venus.unl.edu.ar/gtm-eng.html |


Reply | Threaded
Open this post in threaded view
|

Re: Octave compared to Matlab

John W. Eaton-6
On 14-Aug-1998, Mario Storti <[hidden email]> wrote:

| > octave> ignore_function_time_stamp="all";
| > octave> tic; for k=1:n;   c(k,:)=cross(a(k,:),b(k,:)); endfor; toc
| > ans = 21
| > octave> ignore_function_time_stamp="system";
| > octave> tic; for k=1:n;   c(k,:)=cross(a(k,:),b(k,:)); endfor; toc
| > ans = 21
|
| BTW, apparently, time stamp checking is not too much comsuming in this
| case.

With Octave 2.0.13 (which you say you are using) the time stamps on .m
files should only be checked at most once between the printing of a
prompt, so I would expect similar results in both cases.

You may also be able to get more accurate CPU time results if you use
the cputime function instead of tic and toc.  The cputime function
measures CPU time used by Octave but tic and toc measure wall-clock
time, which can be much different than the CPU time even on a
moderately loaded system.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: Octave compared to Matlab

John W. Eaton-6
In reply to this post by Mario Storti-4
On 14-Aug-1998, Mario Storti <[hidden email]> wrote:

| (From the Octave manual: File: octave.info,  Node: Function Files)
| >    If you know that your own function files will not change while you
| > are running Octave, you can improve performance by setting the variable
| > `ignore_function_time_stamp' to `"all"', so that Octave will ignore the
| > time stamps for all function files.  Setting it to `"system"' gives the
| > default behavior.  If you set it to anything else, Octave will check
| > the time stamps on all function files.
|
| I never used it. I think that it should be used with care. Perhaps
| inside some construct try-catch-end_try_catch (see the command try).

Temporarily resetting built-in variables should be done inside
unwind_protect blocks, like this:

  old_value = built_in_variable;
  unwind_protect
    built_in_variable = "new-value";
    ... code that depends on special built-in variable setting here ...
  unwind_protect_cleanup
    built_in_variable = old_value;
  end_unwind_protect

This way, the value of the built-in variable will be reset no matter
how control exits the block (normal execution, error, or interrupt).

The problem is that you may have to be careful if you reset a built-in
variable and then call another function, because the functions you
call may not be written so that they will work correctly no matter
what the values of the built-in variables are.

(I originally thought that controlling Octave's behavior with various
built-in variables was a good idea.  Now I'm not so sure...)

jwe


Reply | Threaded
Open this post in threaded view
|

Re: Octave compared to Matlab

Mario Storti-4
In reply to this post by John W. Eaton-6

>>>>> On Mon, 17 Aug 1998 23:26:03 -0500 (CDT),
>>>>>      "John W. Eaton" <[hidden email]> said:

> On 14-Aug-1998, Mario Storti <[hidden email]> wrote:
> | > octave> ignore_function_time_stamp="all";
> | > octave> tic; for k=1:n;   c(k,:)=cross(a(k,:),b(k,:)); endfor; toc
> | > ans = 21
> | > octave> ignore_function_time_stamp="system";
> | > octave> tic; for k=1:n;   c(k,:)=cross(a(k,:),b(k,:)); endfor; toc
> | > ans = 21
> |
> | BTW, apparently, time stamp checking is not too much comsuming in this
> | case.

> With Octave 2.0.13 (which you say you are using) the time stamps on .m
> files should only be checked at most once between the printing of a
> prompt, so I would expect similar results in both cases.

I see.

> You may also be able to get more accurate CPU time results if you use
> the cputime function instead of tic and toc.  The cputime function
> measures CPU time used by Octave but tic and toc measure wall-clock
> time, which can be much different than the CPU time even on a
> moderately loaded system.

> jwe

Question: Is the time spent in checking a time stamp counted as CPU
time?

Mario


Reply | Threaded
Open this post in threaded view
|

Re: Octave compared to Matlab

John W. Eaton-6
On 18-Aug-1998, Mario Storti <[hidden email]> wrote:

| Question: Is the time spent in checking a time stamp counted as CPU
| time?

 - Built-in Function: [TOTAL, USER, SYSTEM] = cputime ();
     Return the CPU time used by your Octave session.  The first output
     is the total time spent executing your process and is equal to the
     sum of second and third outputs, which are the number of CPU
     seconds spent executing in user mode and the number of CPU seconds
     spent executing in system mode, respectively.  If your system does
     not have a way to report CPU time usage, `cputime' returns 0 for
     each of its output values.  Note that because Octave used some CPU
     time to start, it is reasonable to check to see if `cputime' works
     by checking to see if the total CPU time used is nonzero.

Octave calls stat(2) to get the time stamp information.  The time
spent executing stat should be counted as `system' time.  So yes, if
you just get the total CPU time, you will also see the time spent
checking time stamps, opening, reading, and writing files, etc.

I usually use something like

  t = cputime ();
  ... code ...
  elapsed = cputime () - t;

to get elapsed CPU time info for a section of code.

jwe