Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Working with big files, I have to convert some numbers that are in string format into real MMA numbers. I know that I can use ToExpression but it's slow when compared to another forms.

For instance, for Integer case we can compare:

dataInt = ToString /@ RandomInteger[1000, {10^5}];
d1 = FromDigits /@ dataInt; // AbsoluteTiming
d2 = ToExpression /@ dataInt; // AbsoluteTiming

{0.050298, Null}
{0.475669, Null}

It's almost 10 times faster to use FromDigits. The question is, how can I make a equivalent for Real cases? Like:

dataReal = ToString /@ RandomReal[1000, {10^5}];
d1 = someFunction/@dataReal;//AbsoluteTiming
d2 = ToExpression/@dataReal;//AbsoluteTiming

I haven't found some Mathematica function to someFunction. I miss one way to force ToExpression to interpret the string in some specific way.

share|improve this question

marked as duplicate by rm -rf Feb 15 '13 at 5:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
This begs the question as to why you aren't just reading the numbers as ... numbers, ReadList[] style. –  dwa Feb 15 '13 at 3:48
1  
I have numbers with comma as separator like "23,23", so I have to use StringReplace and then convert into number. –  Murta Feb 15 '13 at 3:49
    
Can you read as import as "Table"? –  s0rce Feb 15 '13 at 3:49
    
Import is slow, I use ReadList that hasn't this option. –  Murta Feb 15 '13 at 3:53
add comment

2 Answers 2

I found in this answer the function Internal`StringToDouble that does exactly what I was looking for:

dataReal = ToString /@ RandomReal[1000, {10^5}];
d1 = Internal`StringToDouble /@ dataReal; // AbsoluteTiming
d2 = ToExpression /@ dataReal; // AbsoluteTiming
d1 == d2
{0.045391, Null}
{0.544008, Null}
True

As you can see, 10x faster then ToExpression.

share|improve this answer
add comment

Well yours is better and faster but I'll post this anyways. This is the best I can do for the moment. For 10^6 values its about 15% faster then ToExpression:

First@ImportString[
    StringReplace[StringDrop[StringDrop[ToString[dataReal], 1], -1], 
     "," -> ""], "Table"]; // AbsoluteTiming
share|improve this answer
    
tks @S0rce! I haven't sow ImportString, it has some Real options. –  Murta Feb 15 '13 at 4:01
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.