Very slow graph walking My code is very very slow. Could you give me hints on how I can make it much faster?

for (stdext::hash_map&lt;unsigned __int32, CVertex*&gt;::iterator it1 = graph.begin(); it1 != graph.end(); it1++)
{
    for (std::vector&lt;CVertex*&gt;::iterator it2 = it1-&gt;second-&gt;getNeighbors().begin(); it2 != it1-&gt;second-&gt;getNeighbors().end(); it2++)
    {
        if ((*it2)-&gt;isEqualTo(it1-&gt;second)) continue;
        for (std::vector&lt;CVertex*&gt;::iterator it3 = (*it2)-&gt;getNeighbors().begin(); it3 != (*it2)-&gt;getNeighbors().end(); it3++)
        {
            if ((*it3)-&gt;isEqualTo(*it2) || (*it3)-&gt;isEqualTo(it1-&gt;second)) continue;
            for (std::vector&lt;CVertex*&gt;::iterator it4 = (*it3)-&gt;getNeighbors().begin(); it4 != (*it3)-&gt;getNeighbors().end(); it4++)
            {
                if ((*it4)-&gt;isEqualTo(*it3) || (*it4)-&gt;isEqualTo(*it2) || (*it4)-&gt;isEqualTo(it1-&gt;second)) continue;
                for (std::vector&lt;CVertex*&gt;::iterator it5 = (*it4)-&gt;getNeighbors().begin(); it5 != (*it4)-&gt;getNeighbors().end(); it5++)
                {
                     if ((*it5)-&gt;isEqualTo(*it4) || (*it5)-&gt;isEqualTo(*it3) || (*it5)-&gt;isEqualTo(*it2) || (*it5)-&gt;isEqualTo(it1-&gt;second)) continue;
                     for (std::vector&lt;CVertex*&gt;::iterator it6 = (*it5)-&gt;getNeighbors().begin(); it6 != (*it5)-&gt;getNeighbors().end(); it6++)
                     {
                         if (it1-&gt;second-&gt;isEqualTo(*it6))
                         {
                             unsigned __int32 *circle = new unsigned __int32[5];

                             circle[0] = it1-&gt;second-&gt;getWord();
                             circle[1] = (*it2)-&gt;getWord();
                             circle[2] = (*it3)-&gt;getWord();
                             circle[3] = (*it4)-&gt;getWord();
                             circle[4] = (*it5)-&gt;getWord();

                             m_results.push_back(circle);
                         }
                     }
                }
            }
        }
    }
}


I think this question is not subjective because I've found many good things on the Internet yet:


I use only vectors which is much faster (the not reserved too) because of the cache.
You cannot see here, but I use only inline function in vertex class.
I tried to reduce the numbers into uint 32 bit.


But I still miss good trick because its time is lifetime. For example, do you think I can make better compiler settings?

Or it's also very useful if you say I cannot make it much faster because in that case I don't waste more energy into finding a better solution.
Since you've only posted partial code, all we can do is offer some suggestions:


Google suggests that the performance of stdext::hash_map might be improved by adding #define _SECURE_SCL 0. See this MSDN article for background.
Your if's look like they might become victim to branch misprediction. This StackOverflow answer explains that better than I ever could.
If you haven't done so already, take care to allocate your vertices (and their internal data) contiguously. The simplest way to do that is usually to shove everything into a vector.