Efficiency

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

I wrote following finction to exttract items from AD.NET connection string.
For example:

SplitCommaAssignments(cn.ConnectionString,";","=")

return a hash table contains all items in connection string.

I have suspission about the way that i create string array and leave it to
garbage collection inside the for loop. Is it efficient? Does this funcrion
has any performance problem?

Thanks,
Ali



public static System.Collections.Hashtable SplitCommaAssignments(string
str,string strSepparator,string strAssignment)
{
string[] a= str.Split (strSepparator.ToCharArray() );
string[] b;
System.Collections.Hashtable h = new System.Collections.Hashtable ();
for(int i = 0;i<a.GetLength (0);++i)
{
b = a.Split(strAssignment.ToCharArray ());
if (b.GetLength (0)== 2)
{
h.Add(b[0],b[1]);
}
}
return h;
}
 
Hi Ali,

Based on my understanding, you need some information about your string
operation performance of your function.

After review your code, I did not see any big performance problem in it. I
think it should behave well.

For your statement of "leave it to garbage collection inside the for loop",
I think you may have some confusion about the GC.

Actually, in .Net, normally, you need not focus much concern on the memory
management, CLR will invoke GC and manage the memory well for you. That is
why .Net code is called managed code. The time when the GC will break in
and collect the "garbage" is not defined, it all is managed by the CLR.

For more information, please refer to 2 good articles from Jeffrey Richter:
http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/default.aspx

You may also Forcing a Garbage Collection through GC.Collect method, but
this may result in a big performance issue. For more information, please
refer to:
"Forcing a Garbage Collection"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconforcingcollection.asp

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
A.M said:
I wrote following finction to exttract items from AD.NET connection string.
For example:

SplitCommaAssignments(cn.ConnectionString,";","=")

return a hash table contains all items in connection string.

I have suspission about the way that i create string array and leave it to
garbage collection inside the for loop. Is it efficient? Does this funcrion
has any performance problem?

Well, you're calling strAssignment.ToCharArray() each time in the loop,
which isn't helpful. Other than that, it's not too bad - but if you
want to avoid creating the array, you could always do the parsing "by
hand", using IndexOf.

Still using Split, I'd rewrite your code as:

public static Hashtable SplitAssignments (string data,
string pairSeparators,
string keyValueSeparators)
{
string[] pairs = data.Split(pairSeparators.ToCharArray());
char[] keyValueSeparatorChars = keyValueSeparators.ToCharArray();

Hashtable ret = new Hashtable();

foreach (string pair in pairs)
{
string[] parts = pair.Split(keyValueSeparatorChars);
if (parts.Length==2)
{
ret[parts[0]] = parts[1];
}
}

return ret;
}

There's one semantic difference in the above - by using the indexer
rather than Add, the code above doesn't throw an exception if the same
key is used twice. If you want exceptions for invalid data, I suggest
you probably want to throw an exception if the number of parts isn't 2,
as well.
 
Hi Ali,

Does our reply make sense to you?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Can you do something abt the English part of the question so that I
don't need to use the HairSplit method of my system to understand what
you are looking for ?!

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top