How do I optimize schemaDocument.Namespaces code for performance?
I have this code that is called thousands of times and I need to optimize it for performance. I thought about caching xmlQualifiedNames but it's not good enough. any ideas ?
private static string GetPrefixForNamespace(string ns, XmlSchema schemaDocument)
{
string prefix = null;
XmlQualifiedName[] xmlQualifi开发者_如何学PythonedNames = schemaDocument.Namespaces.ToArray();
foreach (XmlQualifiedName qn in xmlQualifiedNames)
{
if (ns == qn.Namespace)
{
prefix = qn.Name;
break;
}
}
return prefix;
}
since you're looking for strings (Namespace
) inside the xmlQualifiedNames
, how about caching those?
Or using LINQ to search in them?
Or - depending on the kind of input you get - using memoization to speed up your calls (really just fancy caching) like in this article.
Stuff it in a Dictionary
or Hashtable
or even a some caching mechanism.
精彩评论