StringBuilder and PCHAR[]

  • Thread starter Thread starter Schley Andrew Kutz
  • Start date Start date
S

Schley Andrew Kutz

I am iterating through 454 rows of a table and with each row I use the ID
field to form a URI for a contact in a public folder on my exchange server.

My memory keeps growing though. I think it is because I reconstruct my URI
each time. I have done this 2 ways and I want to know something. How is
this ...

StringBuilder sb = new StringBuilder( 255, 255 );
for ( int x = 0; x < rs.Rows.Count; ++x )
{
if ( sb.Length > 0 ) sb.Remove( 0, sb.Length );
sb.AppendFormat( "{0}/{1}.EML", URI_PREFIX, rs.Rows[ x ][ "ID" ] );
...
}

any more efficient than this

for ( int x = 0; x < rs.Rows.Count; ++x )
string uri = string.Format( "{0}/{1}.EML", URI_PREFIX, rs.Rows[ x ][
"ID" ] );

The first snippet should be far more effective since I am pre-allocating my
buffer. However, since the ADODB.Connection object takes a string as its
connection source and string are immutable in dot net, whenever I call the
ToString method of the StringBuilder to give to the connection object, it is
going to allocate new memory for that string, correct?

I have found no way in C# to make sequential string operations occupy the
same memory. This is just plain wasteful.

Where in the hell then is the efficiency in this? Why is this better than C
or C++ where you can pass a pointer to an array of char so that you do not
re-create a string in memory from a buffer you pre-allocated for this very
reason to give to a function?

-a
 
Schley said:
I am iterating through 454 rows of a table and with each row I use the ID
field to form a URI for a contact in a public folder on my exchange server.

My memory keeps growing though. I think it is because I reconstruct my URI
each time. I have done this 2 ways and I want to know something. How is
this ...

StringBuilder sb = new StringBuilder( 255, 255 );
for ( int x = 0; x < rs.Rows.Count; ++x )
{
if ( sb.Length > 0 ) sb.Remove( 0, sb.Length );
sb.AppendFormat( "{0}/{1}.EML", URI_PREFIX, rs.Rows[ x ][ "ID" ] );
...
}

any more efficient than this

for ( int x = 0; x < rs.Rows.Count; ++x )
string uri = string.Format( "{0}/{1}.EML", URI_PREFIX, rs.Rows[ x ][
"ID" ] );

The first snippet should be far more effective since I am pre-allocating my
buffer. However, since the ADODB.Connection object takes a string as its
connection source and string are immutable in dot net, whenever I call the
ToString method of the StringBuilder to give to the connection object, it is
going to allocate new memory for that string, correct?

I have found no way in C# to make sequential string operations occupy the
same memory. This is just plain wasteful.

Where in the hell then is the efficiency in this? Why is this better than C
or C++ where you can pass a pointer to an array of char so that you do not
re-create a string in memory from a buffer you pre-allocated for this very
reason to give to a function?

-a

In the particular case you have provided, I dont think the two loops are
similar in efficiency. In fact, I think the first one might be a bit
slower since it needs to do a Remove() before appending. If you are
getting this from the table, you might want to consider doing right in
the database, if the URI_PREFIX is a constant, you can probably write a
SQL function or s select statement which appends both and returns the
resulting row..
Of course, I am not sure it performs much better..
 
Back
Top