Why is str2double so slow in matlab as compared to a mex-function?

1) str2double is a pretty complex function that can convert many different formats. I guess that your mex implementation is much simpler and that can explain why it's faster. Som examples are provided in matlab help.

1) str2double is a pretty complex function that can convert many different formats. I guess that your mex implementation is much simpler and that can explain why it's faster. Som examples are provided in matlab help: str2double('123.45e7') str2double('123 + 45i') str2double('3.14159') str2double('2.7i - 3.14') str2double({'2.71' '3.1415'}) str2double('1,200.34') 2) why mex are faster?

Because when you execute a standard m-file script, basically there is a program that will read your program an execute it (an interpreter). So there are two layers. However, when you write a mex-file, you compile it directly in the CPU language, such that it can be run directly by the processor.

There is only one layer, so it's faster. For more details, see the wikipedia article: en.wikipedia.org/wiki/Interpreted_language en.wikipedia.org/wiki/Compiled_language 3) You cannot see the code of str2double because it is compiled. Matworks does not provide the code of this function.

You can execute it, but not read it. This is the same for all built-in functions.

But it's not the compile time that's causing all it to run slowly, it's the size of the data set. Your first explanation seemed pertinent, but the only lost functionality is that the program can't interpret complex numbers. On the other hand, it has some added functionality, so imo these two would cancel out.

Perhaps it's because matlab stores strings as vectors and this is an inefficient method to do so? – NONE Dec 2 '11 at 22:43.

The implementation of str2double is not hidden from you. To see it, type edit str2doulbe.m. You can also run the profiler on your code to see where in the function all the time is being spent.

Looking at the function, my guess is that it is slow because sscanf is being called inside a loop. One of the commenters in the fileexchange link you posted suggested using the following code to take advantage of sscanf being vectorized: d = reshape(sscanf(sprintf('%s#', c{:}), '%g#'), size(c)); This is actually much faster than str2double for a cell array.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions