[Index for tmp_for_tar/misc] [Return to Master Index]

compare_speed_ncdiff

(tmp_for_tar/misc/compare_speed_ncdiff.m)


Function Synopsis

d = df1(y,x,z)

Help text


 Compare speeds of 'cdiff' + subsequent eval, speed the function
 returned by cdiff and speed of ndiff.

 Last modified: April 2001



Listing of function file tmp_for_tar/misc/compare_speed_ncdiff.m

##
## Compare speeds of 'cdiff' + subsequent eval, speed the function
## returned by cdiff and speed of ndiff.

## Author:        Etienne Grossmann  <etienne@isr.ist.utl.pt>
## Last modified: April 2001

cnt = 0 ;

ntests = 20 ;

function a = f1(y,x,z)
  ## Discard every (y+1)th row
  ## x,y,z
  x = x(complement(y+1:y+1:size(x,1),1:size(x,1)),:);
  b = kron(ones(size(x)./[y,z]),reshape(1:y*z,y,z)) ;
  a = (x.*x).*b;
endfunction

function d = df1(y,x,z)
  d = 2*diag(x(:)) ;
  sz = max(1,size(x,1)-1) ;
  if sz!=1,
    d = d( reshape(1:prod(size(x)),size(x))(1:sz,:)(:), : ) ;
  end
  tmp = find(d) ;
  d(tmp) = d(tmp) .* [1:prod(size(tmp))]' ;
endfunction

sz = [5,7] ;
x = randn(sz) ;
y = size(x,1)-1 ;
z = size(x,2) ;
dx = 1e-6 ;			# default value

d0 = df1(y,x,z) ;

cnt++ ;

printf(["Will time various differentiation functions\n",\
	"  df1        : time taken by hand-made differentiation function\n",\
	"  cdiff+eval : Time taken by call to 'cdiff' and subsequent eval()\n",\
	"  Df1        : Time taken by the function defined cdiff's output\n",\
	"  ndiff      : Time taken by call to 'ndiff'\n"]);

chrono = zeros(3*ntests,4) ;	# cdiff+eval, Df1, ndiff
######################################################################
options = ["cstack";"rstack";"nostack"] ;
for j=1:3,
  opt = deblank(sprintf("%s",options(j,:)));
  mytic() ;
  for i = 1:ntests,
    d0 = df1(y,x,z);
  end
  chrono(j,1) = mytic() ;
  mytic() ;
  for i = 1:ntests,
    c1 = cdiff("f1",2,3,"Df1",opt,dx) ;
    eval(c1) ;
  end
  chrono(j,2) = mytic() ;
  mytic();
  for i = 1:ntests,
    d1 = Df1(y,x,z);
  end
  chrono(j,3) = mytic() ;
  mytic();
  for i = 1:ntests,
    d2 = ndiff("f1","wrt",2,opt,"dx",dx,y,x,z) ;
  end
  chrono(j,4) = mytic() ;
  printf("Option %8s : df1:%-6.4g  cdiff+eval:%-6.4g,   Df1:%-6.4g,   ndiff:%-6.4g\n",\
	 sprintf("'%s'",opt),chrono(j,:)/ntests) ;
end


Produced by oct2html on Sat Apr 28 21:14:54 2001
Cross-Directory links are: ON