C#双到字符数组或替代 [英] c# double to character array or alternative

查看:123
本文介绍了C#双到字符数组或替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储一串包含double类型的许多成员结构实例的计划。几乎每隔一段时间,我倾倒这些到一个文件,这是我做例如使用字符串生成器

I have a program which stores a bunch of structs instances containing many members of type double. Every so often i dump these to a file, which I was doing using string builder e.g. :

 StringBuilder builder = new StringBuilder(256);

 builder.AppendFormat("{0};{1};{2};", x.A.ToString(), x.B.ToString(), x.C.ToString()); 



其中x是我喜欢的类型的实例,而A,B,C为X的成员类型为double。我呼吁每一项的ToString()来避免装箱。然而,这些调用ToString方法还是分配大量内存我的应用程序的情况下,我想减少这种。我在想什么是有一个字符数组,并写入每个成员直接到该,然后创建从字符数组一根弦,并转储到文件中。几个问题:

where 'x' is an instance of my type, and A,B,C are members of X of type double. I call ToString() on each of these to avoid boxing. However, these calls to ToString still allocate lots of memory in the context of my application, and i'd like to reduce this. What i'm thinking is to have a character array, and write each member direct into that and then create one string from that character array and dump that to the file. Few questions:

1)请问我在想什么做的声音合理?有什么人都知道的,将已经实现类似的东西吗?

1) Does what i'm thinking of doing sound reasonable? Is there anything anyone knows of that would already achieve something similar?

2)是否有已建成的东西double转换成字符数组(我猜是高达一些paramaterised精度?)。理想的情况是想在我的阵列和一些指数来传递,并让它开始写入到那里。

2) Is there already something built in to convert double to character array (which i guess would be up to some paramaterised precision?). Ideally want to pass in my array and some index and have it start writing to there.

我试图这样做的原因是为了减少大峰值内存时,我的应用程序正在运行,因为我运行多个实例,并经常发现自己通过内存的限制。

The reason i'm trying to do this is to reduce big spikes in memory when my app is running as I run many instances and often find myself limited by memory.

干杯
A

推荐答案

时所需的文件是某种文本格式的

Is the file required to be some kind of text format?

如果不是,迄今为止最有效的方式来做到这一点是利用一个的BinaryWriter(和BinaryReader读回)。

If not, by far the most efficient way to do this is using a BinaryWriter (and BinaryReader to read them back).

请参阅 HTTP :了解更多信息//msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx

这篇关于C#双到字符数组或替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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