The Difference Between a For and Contains?

Can anyone tell me which version of this code would be better to use?

		for(int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)
		{
			if(Guess[GuessIndex] == HiddenWord[HiddenIndex])
			{
                CowCount++;
                break;
			}
		}

Or

        if(HiddenWord.GetCharArray().Contains(Guess[GuessIndex]))
        {
            CowCount++;
        }        

Both achieve the same end result. I’m just curious if the contains method would run through the entire iteration or break out on finds in other words less efficient?

1 Like

I like the HiddenWord.GetCharArray().Contains(Guess[GuessIndex]) way. It’s using the Unreal functions and is very legible. You can always look at the original function’s definition to understand how it works.
Here’s Contains from Array.h

	bool Contains(const ComparisonType& Item) const
	{
		for (const ElementType* RESTRICT Data = GetData(), *RESTRICT DataEnd = Data + ArrayNum; Data != DataEnd; ++Data)
		{
			if (*Data == Item)
			{
				return true;
			}
		}
		return false;
	}

As you can see, it’s returning immediately once the TCHAR is found, so it should be fairly similar to the manual for loop. Not sure how many million TCHAR the word would need to contain to be able to see a difference in performance between both methods. For this particular use case I would opt for the “short / to the point / easy to read / does the job” Contains option.

2 Likes

Thanks for that NXGEN.

i believe you dont have to use GetCharArray()
you could use HiddenWord.Contains(Guess[GuessIndex])

HiddenWord is FString and Contains is member function. It looks similar but Contains() will find even if the position the character is different so it will be a Cow in our case.

I did a similar approach but i used FindChar(); because it also gives the position where it found.

the function goes thru HiddenWord for Guess[Index] and when it finds it saves the position into variable you pass into which is Position below.
That way you can check whether both HiddenWord and Guess has the same character at the same index . Then it is a Bull :slight_smile:

int32 Position{ 0 };
for (int32 Index = 0; Index < Guess.Len(); ++Index)
{

    HiddenWord.FindChar(Guess[Index], Position);
    if (Position == Index)
        ++Count.Bulls;
    else if (Position != INDEX_NONE)
        ++Count.Cows;
}

}

Don’t have to. Just comparing simplified code and questioning which is better to use. Your approach is also interesting. Thanks.

i think definetly better to use Contains() or FindChar() depending on the use case . Member functions are generally faster or reliable.

both functions stop searching when they found the character but with one you can the position info and the other one does not. So if you dont need that position info then Contains() will be better But in our case since we are checking either a Bull or Cow , you might need that position info to check.

Best way to see how function are implemented when you select the function right click on Peek Definition then you can see the function implementation in a small window underneath your code. Kinda helpfull…I love VS Studio for that :slight_smile: VS Code might have that too.

Nice, Contains is even less code than FindChar. It’s cool that we can get a reference of inner array in C++. C#, for example, would return a new instance in GetCharArray.

Privacy & Terms