tags:

views:

12

answers:

2

Hi, 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[] xmlQualifiedNames = schemaDocument.Namespaces.ToArray();
        foreach (XmlQualifiedName qn in xmlQualifiedNames)
        {
            if (ns == qn.Namespace)
            {
                prefix = qn.Name;
                break;
            }
        }

        return prefix;
    }
A: 

Stuff it in a Dictionary or Hashtable or even a some caching mechanism.

leppie
A: 

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.

Yoni H