将对象转换为字符串的最快方法

编程入门 行业动态 更新时间:2024-10-25 18:25:56
本文介绍了将对象转换为字符串的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好, 我正在尝试使用我们的产品来加速导致性能问题的代码片段b $ b。问题是我们使用序列化将 对象转换为字符串,这会降低我们的性能。有谁知道 任何更好的存档方式,这不会降低性能。代码 是用C#编写的。 谢谢

Hello, I''m trying to speed up a piece of code that is causing performance issues with our product. The problem is we are using serialization to convert the object to a string, this is costing us performance degrade. Does anyone know any better way to archive this which just not degrade performance. The code is written in C#. Thanks

推荐答案

回答你的问题主要取决于:1)字符串 如何与对象相关,以及2)你是如何实际创建字符串的。 要解决(1),我将假设如果你是序列化的话。并且 你的结果是一个字符串,涉及XML。我从来没有尝试过使用StringBuilder和XmlWriter,但是我猜想XmlWriter对于编写格式良好的XML来说效率要高得多。实施 IXmlSerializable,编写代码,对其进行分析,并告诉我它是否仍然太慢。 对于(2),创建字符串有一些提示和技巧。互联网上有很多关于C#字符串实习的文献以及CLR如何处理字符串的b / b 。首先阅读(RTFM)。然后,避免使用 的循环: string myConcatenatedString ="" ;; foreach(myStringList中的字符串myString ) { myConcatenatedString + = myString; } 学习使用StringBuilder对象,String.Format和 String.Concat,以及它们的各种细节,如IFormatProvider。 你可能想要查看StringWriter或其他基于流的设备。 /> 关于你的代码究竟是什么的更多信息会有所帮助。 Stephan Buddy Home写道: The answer to your question depends mostly on: 1) how the string relates to the object, and 2) how you''re actually creating the string. To tackle (1), I''m going to assume that if you''re "serializing" and your result is a string, that there''s XML involved. I''ve never tried StringBuilder vs. XmlWriter, but I''m going to guess that XmlWriter is much more efficient for writing well-formatted XML. Implement IXmlSerializable, write your code, profile it, and let me know if it''s still too slow. For (2), there are a few tips and tricks with creating strings. There''s lots of literature on the Internet about C# string interning and how the CLR treats strings. Read that first (RTFM). Then, avoid loops that look like: string myConcatenatedString = ""; foreach (string myString in myStringList) { myConcatenatedString += myString; } Learn to use the StringBuilder object, String.Format, and String.Concat, along with their various details like IFormatProvider. You may want to look into StringWriter or other stream-based devices. Some more information on what exactly your code does would be helpful. Stephan Buddy Home wrote: 您好, 我正在尝试加速导致性能问题的代码片段 与我们的产品。问题是我们使用序列化将 对象转换为字符串,这会降低我们的性能。有谁知道 任何更好的存档方式,这不会降低性能。代码 是用C#编写的。 谢谢 Hello, I''m trying to speed up a piece of code that is causing performance issues with our product. The problem is we are using serialization to convert the object to a string, this is costing us performance degrade. Does anyone know any better way to archive this which just not degrade performance. The code is written in C#. Thanks

谢谢回复, 首先,我可以说使用序列化通过 BinaryStream将对象转换为字节数组。我使用MemoryStream来存储数据。 我已经知道StringBuilders是如何工作的,并且知道使用String 对象来操作字符串是昂贵。 我的主要问题是,有没有人知道更好的方法。 谢谢, ssamuel < ss ***** @ gmail写信息 news:11 ********************** @ 14g2000cws。 googlegro ups ... Thanks for the reply, First may I say that the object is converted to a byte array via BinaryStream using serialization. I use the MemoryStream to store the data. I''m already aware how StringBuilders works and know that using the String object to manupulate the string is expensive. My main question is that does anyone know a better way to do this. Thanks, "ssamuel" <ss*****@gmailwrote in message news:11**********************@14g2000cws.googlegro ups... 你的问题的答案主要取决于:1)字符串 如何与对象相关, 2)你实际上是如何创建字符串的。 要解决(1),我将假设如果你是序列化的话。并且 你的结果是一个字符串,涉及XML。我从来没有尝试过使用StringBuilder和XmlWriter,但是我猜想XmlWriter对于编写格式良好的XML来说效率要高得多。实施 IXmlSerializable,编写代码,对其进行分析,并告诉我它是否仍然太慢。 对于(2),创建字符串有一些提示和技巧。互联网上有很多关于C#字符串实习的文献以及CLR如何处理字符串的b / b 。首先阅读(RTFM)。然后,避免使用 的循环: string myConcatenatedString ="" ;; foreach(myStringList中的字符串myString ) { myConcatenatedString + = myString; } 学习使用StringBuilder对象,String.Format和 String.Concat,以及它们的各种细节,如IFormatProvider。 你可能想要查看StringWriter或其他基于流的设备。 /> 关于你的代码究竟有什么帮助的更多信息会有所帮助。 Stephan Buddy Home写道: The answer to your question depends mostly on: 1) how the string relates to the object, and 2) how you''re actually creating the string. To tackle (1), I''m going to assume that if you''re "serializing" and your result is a string, that there''s XML involved. I''ve never tried StringBuilder vs. XmlWriter, but I''m going to guess that XmlWriter is much more efficient for writing well-formatted XML. Implement IXmlSerializable, write your code, profile it, and let me know if it''s still too slow. For (2), there are a few tips and tricks with creating strings. There''s lots of literature on the Internet about C# string interning and how the CLR treats strings. Read that first (RTFM). Then, avoid loops that look like: string myConcatenatedString = ""; foreach (string myString in myStringList) { myConcatenatedString += myString; } Learn to use the StringBuilder object, String.Format, and String.Concat, along with their various details like IFormatProvider. You may want to look into StringWriter or other stream-based devices. Some more information on what exactly your code does would be helpful. Stephan Buddy Home wrote: >您好, 我正在尝试加速导致性能问题的代码使用我们的产品。问题是我们使用序列化将对象转换为字符串,这会降低我们的性能。有没有人知道任何更好的存档方式,这不会降低性能。 代码是用C#编写的。 谢谢 >Hello,I''m trying to speed up a piece of code that is causing performance issueswith our product. The problem is we are using serialization to converttheobject to a string, this is costing us performance degrade. Does anyoneknowany better way to archive this which just not degrade performance. Thecodeis written in C#.Thanks

我不能告诉你它是否更快,但你可能想看看 BitConverter.ToString()...... 11月29日下午2:41,Buddy Home < BuddyH ... @ Homewrote: I can''t tell you if it''s faster, but you may want to check out BitConverter.ToString()... On Nov 29, 2:41 pm, "Buddy Home" <BuddyH...@Homewrote: 感谢您的回复, 首先,我可以说,使用序列化通过 BinaryStream将对象转换为字节数组。我使用MemoryStream来存储数据。 我已经知道StringBuilders是如何工作的,并且知道使用String 对象来操作字符串是昂贵。 我的主要问题是,是否有人知道更好的方法。 Thanks for the reply, First may I say that the object is converted to a byte array via BinaryStream using serialization. I use the MemoryStream to store the data. I''m already aware how StringBuilders works and know that using the String object to manupulate the string is expensive. My main question is that does anyone know a better way to do this.

更多推荐

将对象转换为字符串的最快方法

本文发布于:2023-11-10 03:10:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574190.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串   对象   最快   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!