C++ code performance strings compare
<p>I have an array of struct (arrBoards) which has some integer values, vector and a string type.
I want to compare if certain string in the struct is equal with entered parameter (string p1).</p>

<p>What idea is faster - to check equation of input string with every string element inside an array, or firstly check if string.length() in current string element of the array greater than 0, then compare the strings.</p>

<pre><code>if (p1.length())
{
    transform(p1.begin(), p1.end(), p1.begin(), ::tolower); //to lowercase
    for (int i=0; i&lt;arrSize; i++) //check if string element already exists
        if ( rdPtr-&gt;arrBoards[i].sName == p1 )
        {
            */ some code */
            break;
        }
}

if (p1.length())
{
    transform(p1.begin(), p1.end(), p1.begin(), ::tolower); //to lowercase
    for (int i=0; i&lt;arrSize; i++) //check if string element already exists
        if ( rdPtr-&gt;arrBoards[i].sName.length() ) //check length of the string in the element of the array
            if ( rdPtr-&gt;arrBoards[i].sName == p1 )
            {
                */ some code */
                break;
            }
}
</code></pre>

<p>I think the second idea is better because it don't need to calculate the name everytime, but I can be wrong because using if could slow down code.</p>

<p>Thanks for the answers</p>