最快到字符串数组转换为双阵列方式?数组、阵列、快到、字符串

2023-09-03 06:13:20 作者:阳光下你的笑

我一直有与转换大字符串数组与数阵列最近处理,我想知道什么是最快的方法,在那里这确实是。

起初,我通过了:

 双[]双打= sarray.Split('')选择(Double.Parse).ToArray()。
 

...这是真的的甜的...但是今天,我决定切换回一个简单的for循环解析阵列中的所有字符串到双并没有太多令人惊讶的基准似乎赞成在fo​​r循环..所以我应该切换回一个基本的for循环?

另外,我想知道是否有更好的类型,可用于存储分裂字符串如的HashSet 可这个转换过程中有更好的表现?

解决方案

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

与LINQ的 .ToArray(),这pre-分配一个正确大小的数组,并没有做任何调整大小。 这应该是从手卷圈没有什么区别。

LabView中数组的使用2 1

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?

解决方案

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.