octave-maintainers
[Top][All Lists]
Advanced

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

Re: More octave-forge functions!!!


From: David Bateman
Subject: Re: More octave-forge functions!!!
Date: Tue, 30 May 2006 21:55:16 +0200
User-agent: Mozilla Thunderbird 1.0.6-7.6.20060mdk (X11/20050322)

Alois Schloegl wrote:
> Bill Denney wrote:
> 
>> On Mon, 29 May 2006, David Bateman wrote:
>>
>>> For csvread, dlmread, csvwrite, dlmwrite it seems to me there must be a
>>> much simpler way of implementing these efficiently in a script file
>>> without the need for an oct-file.
>>
>>
>>
>> I have some m-file versions of csv2cell and cell2csv that I wrote
>> independently of the o-f versions.  I'll check to see if they look
>> usable for csvread, dlmread, csvwrite, and dlmwrite.
>>
>> Bill
>>
> 
> 
> STR2DOUBLE should be very useful for implementing CSVREAD and DLMREAD
> 
> function m = cvsread(filename)
>    fid =fopen(filename,'r');
>    s= char(fread(fid,inf,'char'));
>    fclose(s);
>    m = str2double(s);
> endfunction
> 
> 
> 
> Alois
> 
> 

Alois,

Not to critisize your code but try running the attached code snippet

a = randn(128,128);
fid=fopen("test.txt",'w');
n = size(m,1);
for i=1:n,
  fprintf(fid,"%8.5f", m(i,1));
  fprintf(fid,", %8.5f",m(i,2:end));
  fprintf(fid,"\n");
endfor;
fclose(fid);
tic; m = dlmread("test.txt"); toc
tic;
fid = fopen("test.txt",'r');
s = char (fread (fid, inf, 'char')');
fclose (fid);
toc;
tic; m = str2num(s); toc
tic; m = str2double(s);

Which returns

Elapsed time is 0.150108 seconds.
Elapsed time is 0.389275 seconds.
Elapsed time is 0.603859 seconds
warning: implicit conversion from string to real N-d array
warning: implicit conversion from string to real N-d array
<snip>
warning: implicit conversion from string to real N-d array
warning: implicit conversion from string to real N-d array
Elapsed time is 36.732208 seconds.

So the oct-file version of dlmread takes 0,16 seconds, a version with
str2num would take 1 second and with str2double would take 37 seconds.
As well there seems to be a large number of bogus warnings. Basically I
think 1 sec is reasonable for the cost of not being in an oct-file and
the overhead should go down with larger files. But all of the additional
processing in str2double, seems to be quite costly. If your problem with
str2num is allowing something like

s = "(system ('rm -fr *'))"
m = str2num(s)

[PLEASE DON'T RUN THE ABOVE!!!]

then why not just create a local version of system (and perhaps a couple
of other functions) that would force the eval to not permit calls to
"system". That is have str2num add at the end

function system (varargin)
  error("system: Not permitted in this context");
endfunction

that should prevent your concerns, at a lower cost...

Regards
David


reply via email to

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