help-octave
[Top][All Lists]
Advanced

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

Re: matlab/octave performance hit


From: David Bateman
Subject: Re: matlab/octave performance hit
Date: Mon, 22 Oct 2007 12:22:33 +0200
User-agent: Thunderbird 1.5.0.7 (X11/20060921)

David Bateman wrote:
> John could we and should we try to intercept tic/toc and cputime in the
> parser and hand the calculation off immediately to builtin versions of
> these functions like the attached?
>   
No need to change the parser. I just tried the attached patch that makes
tic, toc and cputime built-in functions.. The speed is then

octave:1> tic; toc
Elapsed time is 2.00272e-05 seconds.

and for the cumsum problem of Victor

octave:2> t=1:200;tic;cumsum(t);toc
Elapsed time is 0.000160933 seconds.
octave:3> t=1:200;tic;cumsum(t);toc
Elapsed time is 8.39233e-05 seconds.

Note the slower first call. This is much more acceptable.. It seems that
the function table code in Octave 2.9.x is slow except for builtins
(note cumsum is a built-in).. I wonder what the object branch with its
changed function table code can make of this issue. In any case, I'd
suggest applying the attached patch, to avoid as many as possible the
"Octave is too slow" comments..

D.

-- 
David Bateman                                address@hidden
Motorola Labs - Paris                        +33 1 69 35 48 04 (Ph) 
Parc Les Algorithmes, Commune de St Aubin    +33 6 72 01 06 33 (Mob) 
91193 Gif-Sur-Yvette FRANCE                  +33 1 69 35 77 01 (Fax) 

The information contained in this communication has been classified as: 

[x] General Business Information 
[ ] Motorola Internal Use Only 
[ ] Motorola Confidential Proprietary

*** ./scripts/miscellaneous/cputime.m.orig9     2007-10-13 06:52:11.091002231 
+0200
--- ./scripts/miscellaneous/cputime.m   2007-10-22 12:12:13.449947511 +0200
***************
*** 1,49 ****
- ## Copyright (C) 1995, 1996, 1997, 1999, 2005, 2007 John W. Eaton
- ##
- ## This file is part of Octave.
- ##
- ## Octave is free software; you can redistribute it and/or modify it
- ## under the terms of the GNU General Public License as published by
- ## the Free Software Foundation; either version 3 of the License, or (at
- ## your option) any later version.
- ##
- ## Octave is distributed in the hope that it will be useful, but
- ## WITHOUT ANY WARRANTY; without even the implied warranty of
- ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- ## General Public License for more details.
- ##
- ## You should have received a copy of the GNU General Public License
- ## along with Octave; see the file COPYING.  If not, see
- ## <http://www.gnu.org/licenses/>.
- 
- ## -*- texinfo -*-
- ## @deftypefn {Function File} address@hidden, @var{user}, @var{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, @code{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 @code{cputime} works by checking to see if the total
- ## CPU time used is nonzero.
- ## @end deftypefn
- 
- ## Author: jwe
- 
- function [total, user, system] = cputime ()
- 
-   if (nargin != 0)
-     warning ("cputime: ignoring extra arguments");
-   endif
- 
-   resource_stats = getrusage ();
- 
-   usr = resource_stats.utime;
-   sys = resource_stats.stime;
- 
-   user = usr.sec + usr.usec / 1e6;
-   system = sys.sec + sys.usec / 1e6;
-   total = user + system;
- 
- endfunction
--- 0 ----
*** ./scripts/time/tic.m.orig9  2007-10-13 06:52:14.328836246 +0200
--- ./scripts/time/tic.m        2007-10-22 12:11:55.008460936 +0200
***************
*** 1,85 ****
- ## Copyright (C) 1996, 1997, 2006, 2007 John W. Eaton
- ##
- ## This file is part of Octave.
- ##
- ## Octave is free software; you can redistribute it and/or modify it
- ## under the terms of the GNU General Public License as published by
- ## the Free Software Foundation; either version 3 of the License, or (at
- ## your option) any later version.
- ##
- ## Octave is distributed in the hope that it will be useful, but
- ## WITHOUT ANY WARRANTY; without even the implied warranty of
- ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- ## General Public License for more details.
- ##
- ## You should have received a copy of the GNU General Public License
- ## along with Octave; see the file COPYING.  If not, see
- ## <http://www.gnu.org/licenses/>.
- 
- ## -*- texinfo -*-
- ## @deftypefn {Function File} {} tic ()
- ## @deftypefnx {Function File} {} toc ()
- ## Set or check a wall-clock timer.  Calling @code{tic} without an
- ## output argument sets the timer.  Subsequent calls to @code{toc}
- ## return the number of seconds since the timer was set.  For example,
- ##
- ## @example
- ## tic ();
- ## # many computations later...
- ## elapsed_time = toc ();
- ## @end example
- ##
- ## @noindent
- ## will set the variable @code{elapsed_time} to the number of seconds since
- ## the most recent call to the function @code{tic}.
- ##
- ## If called with one output argument then this function returns a scalar
- ## of type @code{uint64} and the wall-clock timer is not started.
- ##
- ## @example
- ## @group
- ## t = tic; sleep (5); (double (tic ()) - double (t)) * 1e-6
- ##      @result{} 5
- ## @end group
- ## @end example
- ##
- ## Nested timing with @code{tic} and @code{toc} is not supported.
- ## Therefore @code{toc} will always return the elapsed time from the most
- ## recent call to @code{tic}.
- ##
- ## If you are more interested in the CPU time that your process used, you
- ## should use the @code{cputime} function instead.  The @code{tic} and
- ## @code{toc} functions report the actual wall clock time that elapsed
- ## between the calls.  This may include time spent processing other jobs or
- ## doing nothing at all.  For example,
- ##
- ## @example
- ## @group
- ## tic (); sleep (5); toc ()
- ##      @result{} 5
- ## t = cputime (); sleep (5); cputime () - t
- ##      @result{} 0
- ## @end group
- ## @end example
- ##
- ## @noindent
- ## (This example also illustrates that the CPU timer may have a fairly
- ## coarse resolution.)
- ## @end deftypefn
- 
- ## Author: jwe
- 
- function ret = tic ()
- 
-   if (nargin != 0)
-     warning ("tic: ignoring extra arguments");
-   endif
- 
-   if (nargout == 1)
-     ret = uint64 (time () * 1e6);
-   else
-     global __tic_toc_timestamp__;
-     __tic_toc_timestamp__ = clock ();
-   endif
- 
- endfunction
--- 0 ----
*** ./scripts/time/toc.m.orig9  2007-10-13 06:52:14.342835528 +0200
--- ./scripts/time/toc.m        2007-10-22 12:12:00.875025135 +0200
***************
*** 1,46 ****
- ## Copyright (C) 1996, 1997, 2006, 2007 John W. Eaton
- ##
- ## This file is part of Octave.
- ##
- ## Octave is free software; you can redistribute it and/or modify it
- ## under the terms of the GNU General Public License as published by
- ## the Free Software Foundation; either version 3 of the License, or (at
- ## your option) any later version.
- ##
- ## Octave is distributed in the hope that it will be useful, but
- ## WITHOUT ANY WARRANTY; without even the implied warranty of
- ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- ## General Public License for more details.
- ##
- ## You should have received a copy of the GNU General Public License
- ## along with Octave; see the file COPYING.  If not, see
- ## <http://www.gnu.org/licenses/>.
- 
- ## -*- texinfo -*-
- ## @deftypefn {Function File} {} toc ()
- ## See tic.
- ## @end deftypefn
- 
- ## Author: jwe
- 
- function secs = toc ()
- 
-   if (nargin != 0)
-     warning ("toc: ignoring extra arguments");
-   endif
- 
-   global __tic_toc_timestamp__ = -1;
- 
-   if (__tic_toc_timestamp__ < 0)
-     warning ("toc called before timer set");
-     secs0 = [];
-   else
-     secs0 = etime (clock (), __tic_toc_timestamp__);
-     if (nargout != 0)
-       secs = secs0;
-     else
-       printf ("Elapsed time is %f seconds.\n", secs0);
-     endif  
-   endif
- 
- endfunction
--- 0 ----
*** ./src/data.cc.orig9 2007-10-22 12:11:38.511498451 +0200
--- ./src/data.cc       2007-10-22 12:14:05.200597323 +0200
***************
*** 47,52 ****
--- 47,53 ----
  #include "pt-mat.h"
  #include "utils.h"
  #include "variables.h"
+ #include "pager.h"
  
  #define ANY_ALL(FCN) \
   \
***************
*** 2963,2968 ****
--- 2964,3143 ----
    BINARY_OP_DEFUN_BODY (op_el_or);
  }
  
+ static double __tic_toc_timestamp__ = -1.0;
+ 
+ DEFUN (tic, args, nargout,
+   "-*- texinfo -*-\n\
+ @deftypefn {Built-in Function} {} tic ()\n\
+ @deftypefnx {Built-in Function} {} toc ()\n\
+ Set or check a wall-clock timer.  Calling @code{tic} without an\n\
+ output argument sets the timer.  Subsequent calls to @code{toc}\n\
+ return the number of seconds since the timer was set.  For example,\n\
+ \n\
+ @example\n\
+ tic ();\n\
+ # many computations later...\n\
+ elapsed_time = toc ();\n\
+ @end example\n\
+ \n\
+ @noindent\n\
+ will set the variable @code{elapsed_time} to the number of seconds since\n\
+ the most recent call to the function @code{tic}.\n\
+ \n\
+ If called with one output argument then this function returns a scalar\n\
+ of type @code{uint64} and the wall-clock timer is not started.\n\
+ \n\
+ @example\n\
+ @group\n\
+ t = tic; sleep (5); (double (tic ()) - double (t)) * 1e-6\n\
+      @result{} 5\n\
+ @end group\n\
+ @end example\n\
+ \n\
+ Nested timing with @code{tic} and @code{toc} is not supported.\n\
+ Therefore @code{toc} will always return the elapsed time from the most\n\
+ recent call to @code{tic}.\n\
+ \n\
+ If you are more interested in the CPU time that your process used, you\n\
+ should use the @code{cputime} function instead.  The @code{tic} and\n\
+ @code{toc} functions report the actual wall clock time that elapsed\n\
+ between the calls.  This may include time spent processing other jobs or\n\
+ doing nothing at all.  For example,\n\
+ \n\
+ @example\n\
+ @group\n\
+ tic (); sleep (5); toc ()\n\
+      @result{} 5\n\
+ t = cputime (); sleep (5); cputime () - t\n\
+      @result{} 0\n\
+ @end group\n\
+ @end example\n\
+ \n\
+ @noindent\n\
+ (This example also illustrates that the CPU timer may have a fairly\n\
+ coarse resolution.)\n\
+ @end deftypefn")
+ {
+   octave_value retval;
+   int nargin = args.length ();
+ 
+   if (nargin != 0)
+     warning ("tic: ignoring extra arguments");
+ 
+   if (nargout > 0)
+     retval = static_cast<octave_uint64> (static_cast<double> (octave_time ()) 
* 1.0e6);
+   else
+     __tic_toc_timestamp__= static_cast<double>(octave_time ());
+       
+   return retval;
+ }
+ 
+ DEFUN (toc, args, nargout,
+   "-*- texinfo -*-\n\
+ @deftypefn {Built-in Function} {} toc ()\n\
+ See tic.\n\
+ @end deftypefn")
+ {
+   double sec = static_cast<double>(octave_time ());
+   octave_value retval;
+   int nargin = args.length ();
+ 
+   if (nargin != 0)
+     warning ("tic: ignoring extra arguments");
+ 
+   if (__tic_toc_timestamp__ < 0)
+     {
+       warning ("toc called before timer set");
+       if (nargout > 0)
+       retval = Matrix();
+     }
+   else if (nargout > 0)
+     retval = sec - __tic_toc_timestamp__;
+   else
+     octave_stdout << "Elapsed time is " << sec - __tic_toc_timestamp__ 
+                 << " seconds.\n";
+     
+   return retval;
+ }
+ 
+ DEFUN (cputime, args, ,
+   "-*- texinfo -*-\n\
+ @deftypefn {Built-in Function} address@hidden, @var{user}, @var{system}] =} 
cputime ();\n\
+ Return the CPU time used by your Octave session.  The first output is\n\
+ the total time spent executing your process and is equal to the sum of\n\
+ second and third outputs, which are the number of CPU seconds spent\n\
+ executing in user mode and the number of CPU seconds spent executing in\n\
+ system mode, respectively.  If your system does not have a way to report\n\
+ CPU time usage, @code{cputime} returns 0 for each of its output values.\n\
+ Note that because Octave used some CPU time to start, it is reasonable\n\
+ to check to see if @code{cputime} works by checking to see if the total\n\
+ CPU time used is nonzero.\n\
+ @end deftypefn")
+ {
+   octave_value_list retval;
+   int nargin = args.length ();
+   double usr = 0.0;
+   double sys = 0.0;
+ 
+   if (nargin != 0)
+     warning ("tic: ignoring extra arguments");
+ 
+ #if defined (HAVE_GETRUSAGE)
+ 
+   struct rusage ru;
+ 
+   getrusage (RUSAGE_SELF, &ru);
+ 
+   usr = static_cast<double> (ru.ru_utime.tv_sec) +
+     static_cast<double> (ru.ru_utime.tv_usec) * 1e-6;
+ 
+   sys = static_cast<double> (ru.ru_stime.tv_sec) +
+     static_cast<double> (ru.ru_stime.tv_usec) * 1e-6;
+ 
+ #elif defined (HAVE_TIMES) && defined (HAVE_SYS_TIMES_H)
+ 
+   struct tms t;
+ 
+   times (&t);
+ 
+   unsigned long ticks;
+   unsigned long seconds;
+   unsigned long fraction;
+ 
+   ticks = t.tms_utime + t.tms_cutime;
+   fraction = ticks % HZ;
+   seconds = ticks / HZ;
+ 
+   usr = static_cast<double> (seconds) + static_cast<double>(fraction) /
+     static_cast<double>(HZ);
+ 
+   ticks = t.tms_stime + t.tms_cstime;
+   fraction = ticks % HZ;
+   seconds = ticks / HZ;
+ 
+   sys = static_cast<double> (seconds) + static_cast<double>(fraction) /
+     static_cast<double>(HZ);
+ 
+ #elif defined (__WIN32__)
+   HANDLE hProcess = GetCurrentProcess ();
+   FILETIME ftCreation, ftExit, ftUser, ftKernel;
+   GetProcessTimes (hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);
+ 
+   int64_t itmp = *(reinterpret_cast<int64_t *> (&ftUser));
+   usr = static_cast<double> (itmp) * 1e-1;
+ 
+   itmp = *(reinterpret_cast<int64_t *> (&ftKernel));
+   sys = static_cast<double> (itmp) * 1e-1;
+ 
+ #endif
+ 
+   retval (2) = sys;
+   retval (1) = usr;
+   retval (0) = sys + usr;
+ 
+   return retval;
+ }
+ 
  /*
  ;;; Local Variables: ***
  ;;; mode: C++ ***
2007-10-22  David Bateman  <address@hidden>

        * miscellaneous/cputime.m, time/tic.m, time/toc.m: Delete.

2007-10-22  David Bateman  <address@hidden>

        * data.cc (Ftic, Ftoc, Fcputime): New builtin versions of the
        benchmarking functions for speed.

reply via email to

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