Robert McLaws: FunWithCoding.NET

Public Shared Function BrainDump(ByVal dotNet As String) As [Value]

News

<script type="text/javascript"><!-- google_ad_client = "pub-4330602465258980"; google_hints = "ASP.NET, VB.NET, C#, C#.NET, WindowsForms, .NET Framework, VS2005, Visual Studio, XAML, WinFX, Windows Workflow, WPF, WCF, Atlas, NetFX3, Visual Studio Orcas"; google_ad_width = 120; google_ad_height = 240; google_ad_format = "120x240_as"; google_ad_type = "text_image"; google_ad_channel ="4997399242"; google_color_border = "B6C9E7"; google_color_bg = "EFEFEF"; google_color_link = "0000FF"; google_color_text = "000000"; google_color_url = "002C99"; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<!--
-->

You should feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever. That said, I will most likely only delete abusive, profane, rude, or annonymous comments, so keep it polite, please.

Blogroll

Cool .NET Articles

My .NET Tools

My Builder.com Articles

My MSKB Articles

StringBuilder Size Algorithm

When I posted about this before, someone noted that cycling through all the items would be a performance hit. Well, as you can see, I'm really only cycling thru the tables in the DataSet, and in the case of the DataReader, I'm only accessing a property that already exists.

For DataSets:

Dim FieldCount As Int32 = 0

'Algorithm for setting the minimum StringBuilder size initially, so that we do not have to continually re-allocate memory
'This should increase performance
For Each dt In dataset.Tables
     FieldCount += dt.Columns.Count * dt.Rows.Count
Next
sb.EnsureCapacity(FieldCount * 25)

For DataReaders:

Dim FieldCount As Int32 = 0

'Algorithm for setting the minimum StringBuilder size initially, so that we do not have to continually re-allocate memory
'This should increase performance
sb.EnsureCapacity(dr.FieldCount * 25)

This should be fairly adequate in most situations. You want to undershoot by less than half, because if you can do that, then it will only resize once, having reached it's maximum capacity a little more than halfway through. When the StringBuilder resizes, it doubles it size, so this logic only makes sense. You don't ever want it to resize more than once tho, otherwise you'll have way too much wasted memory.

Posted: Jul 30 2003, 06:51 PM by interscape | with no comments
Filed under:

Comments

No Comments