help-octave
[Top][All Lists]
Advanced

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

patch : exist("varname","var")


From: etienne grossmann
Subject: patch : exist("varname","var")
Date: Thu, 23 Mar 2000 21:17:07 +0000 (WET)

  Hello,
 
  here is a little patch so that exist("varname","var") returns 1 if
there is a variable called "varname" and zero otherwise. No check is
done for builtin functions or files. This is what I, most often, use
"exist" for.

  Acceleration is noticable (the script that produces this output is
  given after the patch) :

======================================================================
octave:2> test_exist_speed_2 
ntests = 20
without exist()      : 0.000000
really simple        : 0.000500
empty loop           : 0.000500
with    exist()      : 0.018500
with    exist('var') : 0.000500
======================================================================


  Cheers,

  Etienne

Changelog entry :
======================================================================
2000-03-23  Etienne Grossmann  <address@hidden>

        * src/variables.cc : Make "exist()" check only for the
          existence of a variable (not function, .oct or file), if a
          second argument is present and equal to "var".
======================================================================

Patch (against 2.1.19):
======================================================================
*** src/variables.cc.orig       Thu Mar 23 20:41:40 2000
--- src/variables.cc    Thu Mar 23 21:00:00 2000
***************
*** 396,402 ****
  }
  
  DEFUN (exist, args, ,
!   "exist (NAME): check if variable or file exists\n\
  \n\
  returns:\n\
  \n\
--- 396,402 ----
  }
  
  DEFUN (exist, args, ,
!   "exist (NAME [,'var']): check if variable or file exists\n\
  \n\
  returns:\n\
  \n\
***************
*** 409,421 ****
  This function also returns 2 if a regular file called NAME exists in\n\
  Octave's LOADPATH.  If you want information about other types of\n\
  files, you should use some combination of the functions file_in_path\n\
! and stat instead.")
  {
    octave_value_list retval;
  
    int nargin = args.length ();
  
!   if (nargin != 1)
      {
        print_usage ("exist");
        return retval;
--- 409,436 ----
  This function also returns 2 if a regular file called NAME exists in\n\
  Octave's LOADPATH.  If you want information about other types of\n\
  files, you should use some combination of the functions file_in_path\n\
! and stat instead. If the second argument is present and equal to \n\
! \"var\", then only the existence of a variable is checked.")
  {
    octave_value_list retval;
  
    int nargin = args.length ();
+   
+   string var_only = "" ;
+   int var_only_i = 0 ;
  
!   if (nargin == 2)
!     {
!       var_only = args(1).string_value ();
!       if( var_only.compare ("var", 0, 3) != 0 )
!       {
!         print_usage ("exist");
!         return retval;
!       }
!       else 
!       var_only_i = 1 ;
!     }
!   if (nargin > 2)
      {
        print_usage ("exist");
        return retval;
***************
*** 451,456 ****
--- 466,475 ----
      {
        if (struct_elts.empty () || sr->is_map_element (struct_elts))
        retval = 1.0;
+     }
+   else if ( var_only_i ) 
+     {
+       return retval ;
      }
    else if (sr && sr->is_builtin_function ())
      {
======================================================================

Test script :
======================================================================
1 ;

function y = foo(x,y,z)
  if exist("z")!=1, z = 1 ; end
  if exist("y")!=1, y = 1 ; end
  if exist("x")!=1, x = 1 ; end
end

function y = foo2(x,y,z)
  if exist("z","var")!=1, z = 1 ; end
  if exist("y","var")!=1, y = 1 ; end
  if exist("x","var")!=1, x = 1 ; end
end

function y = bar(x,y,z)
  if 1, z = 1 ; end
  if 1, y = 1 ; end
  if 1, x = 1 ; end
end

function y = baz(x,y,z) 
end

ntests = 20 

mytic();
for i=1:ntests, bar(1,2); end
t2 = mytic();
printf("without exist()      : %f\n",t2/ntests) ;

mytic();
for i=1:ntests, baz(1,2); end
t3 = mytic();
printf("really simple        : %f\n",t3/ntests) ;

mytic();
for i=1:ntests, end
t4 = mytic();
printf("empty loop           : %f\n",t4/ntests) ;

mytic();
for i=1:ntests, foo(1,2); end
t1 = mytic();
printf("with    exist()      : %f\n",t1/ntests) ;

for i=1:ntests, foo2(1,2); end
t1 = mytic();
printf("with    exist('var') : %f\n",t1/ntests) ;
=====================================================================



-----------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.che.wisc.edu/octave/octave.html
How to fund new projects:  http://www.che.wisc.edu/octave/funding.html
Subscription information:  http://www.che.wisc.edu/octave/archive.html
-----------------------------------------------------------------------



reply via email to

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