cellfunc vs cell2mat speed?

David Bateman dbateman at dbateman.org
Tue Sep 9 15:40:46 CDT 2008


Levente Torok wrote:
> Hi David and others
>
> This is small speed test:
>
> function t = test( s )
>
>     t = time;
>     c=cell( s, 1 );
>     r = rand( 8, 1 );
>     c{:,1} = r;
>     cc = cell2mat( c );
>     t = time - t;
>
> end
>
> octave:4> test( 1000 )
> ans =  0.27966
> octave:5> test( 10000 )
> ans =  12.347
> octave:6> test( 13000 )
> ans =  26.865
> octave:3> test( 16000 )
> ans =  47.761
> octave:4> test( 19000 )
> ans =  83.082
>
>
> As it seems, each adding of 3000 elements doubles the time it needs to convert cell to matrix.
> I believe the slowness is due to the recursive scripting nature of the cell2mat.m function.
> I thought I will write a small conversion program in C with limited capabilities for my own needs 
> ( ie. only c{i,1} =matrix will be handled ) but I am so much confused with the interface of the 
> Cell class.
>
>
> ( NB: if I replace cell2mat with cellfun( c, "mean" ) I would get:
> octave:10> test( 20000 )
> ans =  8.0793
> and this increases linearly with size of the cell array)
> Thank you very much in advance,
>
> Levente
>
>
>
>
>   

The code that handles this is essentially

 t = cputime; cc = cat(1, c{:}); cputime - t

There are two issues with the above. The first is that the cat function 
is called with an enormously large number of arguments and the second is 
that the cat function, unlikely [], doesn't make any specializations for 
the same class of data in the concatenation. Compare the above with

t = cputime; cc = [c{:}]; cputime - t;

I'm working on a fix..


D.


More information about the Help-octave mailing list