changeset 10034:439c7a9d0fca

optimize common_size
author Jaroslav Hajek <highegg@gmail.com>
date Sun, 27 Dec 2009 22:07:17 +0100
parents f349847c4541
children 66d56fb7cbb8
files scripts/ChangeLog scripts/general/common_size.m
diffstat 2 files changed, 14 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Sun Dec 27 21:56:53 2009 +0100
+++ b/scripts/ChangeLog	Sun Dec 27 22:07:17 2009 +0100
@@ -1,3 +1,7 @@
+2009-12-27  Jaroslav Hajek  <highegg@gmail.com>
+
+	* general/common_size.m: Optimize.
+
 2009-12-27  Jaroslav Hajek  <highegg@gmail.com>
 
 	* strings/strcat.m: Vectorize.
--- a/scripts/general/common_size.m	Sun Dec 27 21:56:53 2009 +0100
+++ b/scripts/general/common_size.m	Sun Dec 27 22:07:17 2009 +0100
@@ -1,6 +1,7 @@
 ## Copyright (C) 1995, 1996, 1999, 2000, 2002, 2004, 2005, 2007
 ##               Kurt Hornik
 ## Copyright (C) 2009 VZLU Prague
+## Copyright (C) 2009 Jaroslav Hajek
 ##
 ## This file is part of Octave.
 ##
@@ -69,8 +70,15 @@
       if (nargout > 1)
         scal = !nscal;
         varargout = varargin;
-        varargout(scal) = cellfun (@repmat, varargin(scal), {size(varargin{i})}, ...
-                                   "UniformOutput", false);
+        if (any (nscal))
+          ## This could also be achieved by cellfun (@repmat, ...), but repmat is an
+          ## m-function and hence still carries interpreter overhead. Further,
+          ## cellfun is slightly optimized for the case used below.
+          uo = "uniformoutput";
+          dims = size (varargin{find (nscal, 1)});
+          subs = substruct ("()", arrayfun (@ones, 1, dims, uo, false));
+          varargout(scal) = cellfun (@subsref, varargin(scal), {subs}, uo, false);
+        endif
       endif
     endif
   endif