7

Is there a way to convert a list of string to numbers faster than ToExpression?

str = ToString /@ RandomReal[100, 1000];
ToExpression@str; // AbsoluteTiming // First
(* 0.007714 *)

Related: How to convert string to integer list?

Edit Thanks to J.M. who pointed to this answer:

  Internal`StringToDouble/@str; // AbsoluteTiming // First
 (* 0.000834 *)
anderstood
  • 14,301
  • 2
  • 29
  • 80

1 Answers1

6

I'd just like to point out the J.M.'s answer is also best if you have ints, since Floor is fast and listable as a cast from Real:

intStrings = RandomInteger[10000, 100000] // Map[ToString];

Floor[Internal`StringToDouble /@ intStrings] // RepeatedTiming // First

0.048

ToExpression@intStrings // RepeatedTiming // First

0.413
b3m2a1
  • 46,870
  • 3
  • 92
  • 239
  • Maybe you can incorporate J.M.'s answer too (as community if you're not comfortable) so that I can accept it. – anderstood Jun 11 '18 at 00:39