Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've been having to deal lately with conversion of large string arrays to number arrays and I'm wondering what the fastest method out there for this really is.

At first I adopted:

double[] doubles = sarray.Split(',').Select(Double.Parse).ToArray();

...which is really sweet... But today, I decided to switch back to a simple for loop to parse all strings in array to Double and not too surprisingly the benchmark seemed to favour the for loop.. so should I switch back to a basic for loop?

Also, I want to know if there's a better type that can be used to store the splitted strings e.g. HashSet which may perform better during this conversion?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
214 views
Welcome To Ask or Share your Answers For Others

1 Answer

Array.ConvertAll(sarray.Split(','), Double.Parse);

Unlike LINQ's .ToArray(), this pre-allocates a correctly-sized array and doesn't do any resizing.
This should be indistinguishable from a hand-rolled loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...