octave-maintainers
[Top][All Lists]
Advanced

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

Re: Testing for function handles


From: Juan Pablo Carbajal
Subject: Re: Testing for function handles
Date: Wed, 9 Nov 2011 22:37:09 +0100

On Wed, Nov 9, 2011 at 10:06 PM, John W. Eaton <address@hidden> wrote:
> On  9-Nov-2011, Richard Crozier wrote:
>
> |
> |
> | On 09/11/2011 11:15, Richard wrote:
> | > On 09/11/2011 10:32, Juan Pablo Carbajal wrote:
> | >> On Wed, Nov 9, 2011 at 11:01 AM, Richard<address@hidden>  wrote:
> | >>> On 09/11/2011 08:18, Dr. Alexander Klein wrote:
> | >>>> Good morning,
> | >>>>
> | >>>> is there any preferred way to check whether a given parameter or
> | >>>> variable
> | >>>> contains a function handle?
> | >>>>
> | >>>> I find myself strcmp'ing the result of typeinfo quite often, but
> | >>>> this is
> | >>>> really clumsy. Then again, ishandle only checks for graphics handles
> | >>>> ...
> | >>>>
> | >>>> Best regards,
> | >>>>
> | >>>>         Alex
> | >>>>
> | >>> if it is octave code you can use the 'isa' function e.g.
> | >>>
> | >>> isa(@sin, 'function_handle')
> | >>>
> | >>> but maybe I'm misunderstanding and you mean in the C++ sources or
> | >>> something.
> | >>>
> | >>> Richard
> | >>>
> | >>> --
> | >>> The University of Edinburgh is a charitable body, registered in
> | >>> Scotland, with registration number SC005336.
> | >>>
> | >>>
> | >> Checking
> | >> f = @(x)x;
> | >> n=5e4;
> | >> t0=cputime;
> | >> for i=1:n
> | >>    strcmp(typeinfo(f),'function_handle');
> | >> end
> | >> t=cputime-t0
> | >>
> | >> t0=cputime;
> | >> for i=1:n
> | >>    isa(f,'function_handle');
> | >> end
> | >> t=cputime-t0
> | >>
> | >> I get
> | >> t =  1.0521
> | >> for strcmp
> | >> t =  5.0083
> | >> for isa
> | >>
> | >> Is the benchmark correct?
> | >>
> | >
> | > Yes, well on my machine:
> | >
> | > f = @(x)x;
> | > n=5e4;
> | > tic;
> | > for i=1:n
> | >   strcmp(typeinfo(f),'function_handle');
> | > end
> | > toc
> | >
> | > tic;
> | > for i=1:n
> | >   isa(f,'function_handle');
> | > end
> | > toc
> | >
> | > Yields
> | >
> | > Elapsed time is 0.862049 seconds.
> | > Elapsed time is 3.15218 seconds.
> | > octave-3.2.4.exe:3>
> | >
> | > Although as you can see I'm using an out of date version of Octave
> | >
> | > Not sure why this should be so though, but maybe this is why isa is so
> | > sparsely documented?
> | >
> | >
> |
> | Incidentally the following in Matlab
> |
> | f = @(x)x;
> | n=5e4;
> |
> | tic;
> | for i=1:n
> |   isa(f,'function_handle');
> | end
> | toc
> |
> | Returns (on a different, less powerful, machine):
> |
> | Elapsed time is 0.249872 seconds.
> |
> | On Matlab, isa is not an mfile though. There is also no typeinfo in
> | Matlab (R2008a at least) for comparison.
>
> Try class instead of typeinfo.
>
> Yes, we could add an is_function_handle that could be faster than
> isa.  Would someone like to submit a changeset for that?
>
> Look at isa.m ("type isa" at the Octave prompt) and you'll see that it
> is more complicated than just strcmp (class (x), classname) because it
> has to be able to do things like 'isa (x, "float")'.
>
> Also, why is this thread here instead of the help list?
>
> jwe
>

Hi jwe,
I do not know why this thread is here...thought it was initiated by a
dev... I never checked.
I could work on the implementation of a function to check for
function_handle. I would do something like what is done for ischar,
src/strfns.cc line 297. I have noticed that there is already a
is_function_hanlde in octave_values, so it is just a matter of
defining the function.
Now, where is the place for such a function? and Would you call it
is_function_handle or isfunhandle or something alike?

KaKiLa




-- 
M. Sc. Juan Pablo Carbajal
-----
PhD Student
University of Zürich
http://ailab.ifi.uzh.ch/carbajal/


reply via email to

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