最快到字符串数组转换为双阵列方式? [英] Fastest way to convert string array to double array?

查看:119
本文介绍了最快到字符串数组转换为双阵列方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

起初,我通过了:

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

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

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

解决方案

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

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

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.

这篇关于最快到字符串数组转换为双阵列方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆