Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 R. M. 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
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

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}

As you can see, 10x faster then ToExpression.

share|improve this answer

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:

    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

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