octave-maintainers
[Top][All Lists]
Advanced

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

Re: Testing for function handles


From: Richard Crozier
Subject: Re: Testing for function handles
Date: Wed, 09 Nov 2011 11:52:00 +0000
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1


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.


-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.



reply via email to

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