将浮点格式的char []转换为float [英] Convert a float-formated char[] to float

查看:84
本文介绍了将浮点格式的char []转换为float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个char[] salary,其中包含来自string的数据.我想将char[] salary转换为float,但是按照我尝试的方法,它似乎极其慢:

I have a char[] salary which contains data that comes from a string. I want to convert char[] salary to float, but it seems to be extremelly slow by the method I'm trying, which is:

float ff = float.Parse(new string(salary));

根据Visual Studio的Performance Profiler,此处理量过多:

According to Visual Studio's Performance Profiler this is taking way too much processing:

因此,我想知道是否有更快的方法来执行此操作,因为此处的性能很重要. char[]的格式如下:

So I'd like to know if there's a faster way to do this, Since performance here is a point. The char[] is formated like so:

[ '1', '3, '2', ',' '2', '9']

基本上是一个类似于JSON的浮点型,转换为适合char[]的每个数字(和逗号).

And is basically a JSON-like float converted to every digit (and comma) fit into a char[].

我已经重新格式化了代码,似乎性能下降实际上是从char[]string的转换,而不是从stringfloat的解析.

I've reformatted the code and it seems like the performance hit is actually in the conversion from char[] to string, not the parsing from string to float.

推荐答案

经过一些实验和的测试:

After some experiments and the tests from this:

char[]获取string的最快方法是使用new string

The fastest way to have string from char[] is using new string

在此条款TryParse是解析float的最快方法.因此,请考虑一下.

One more attention FYI, following this article of Microsoft in the case of invalid input, TryParse is the fastest way to parse float. So, think about it..

TryParse仅占用执行时间的0.5%,Parse占用18%的时间,而Convert占用14%的时间

TryParse is only taking .5% of execution time Parse is taking 18% while Convert is taking 14%

这篇关于将浮点格式的char []转换为float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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