Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Recommended FName comparison method

There are several ways to compare FNames. What is the best way regarding performance and if the TCHAR array "CompareToMe" is used temporary just for this comparison?

 existingFName.Compare(FName(TEXT("CompareToMe"), FNAME_Find))
 existingFName == TEXT("CompareToMe")
 existingFName == FName(TEXT("CompareToMe"))
 existingFName == FName(TEXT("CompareToMe"), FNAME_Find)

So is there a difference regarding performance between

 TEXT("CompareToMe") // *
 FName(TEXT("CompareToMe")) // Like above (same constructor called)?
 FName(TEXT("CompareToMe"), FNAME_Find) // Will return NAME_None, if not existent?
  • Works because FName(const [WIDECHAR/ANSICHAR]* Name, EFindName FindType=FNAME_Add) is called -> this adds "CompareToMe" to stringTable (if not present)?



Looking into FName::Compare(), there is done a wide to narrow convert in case of an initial mismatch which I'd like to avoid. Unfortunately I didn't found operator== for FName to try to answer the question for myself.


Product Version: UE 4.14
more ▼

asked Feb 12 '17 at 05:54 PM in C++ Programming

avatar image

Roi Danton
72 10 16 16

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

operator == is more performant than Compare if you're only testing for equality, because that's all it does. From the source:

== is in NameTypes.h:

 FORCEINLINE bool operator==(const FName& Other) const
         return GetComparisonIndexFast() == Other.GetComparisonIndexFast() && GetNumber() == Other.GetNumber();
         static_assert(sizeof(CompositeComparisonValue) == sizeof(*this), "ComparisonValue does not cover the entire FName state");
         return CompositeComparisonValue == Other.CompositeComparisonValue;

Compare is here:

 int32 FName::Compare( const FName& Other ) const
     // Names match, check whether numbers match.
     if( GetComparisonIndexFast() == Other.GetComparisonIndexFast() )
         return GetNumber() - Other.GetNumber();
     // Names don't match. This means we don't even need to check numbers.
         TNameEntryArray& Names = GetNames();
         const FNameEntry* const ThisEntry = GetComparisonNameEntry();
         const FNameEntry* const OtherEntry = Other.GetComparisonNameEntry();
         // Ansi/Wide mismatch, convert to wide
         if( ThisEntry->IsWide() != OtherEntry->IsWide() )
             return FCStringWide::Stricmp(    ThisEntry->IsWide() ? ThisEntry->GetWideName() : StringCast<WIDECHAR>(ThisEntry->GetAnsiName()).Get(),
                                 OtherEntry->IsWide() ? OtherEntry->GetWideName() : StringCast<WIDECHAR>(OtherEntry->GetAnsiName()).Get() );
         // Both are wide.
         else if( ThisEntry->IsWide() )
             return FCStringWide::Stricmp( ThisEntry->GetWideName(), OtherEntry->GetWideName() );
         // Both are ansi.
             return FCStringAnsi::Stricmp( ThisEntry->GetAnsiName(), OtherEntry->GetAnsiName() );

Note that both do precisely the same thing to test for equality, but compare goes on to test for ordering if they aren't equal, whereas == does not. So in the case of equality their performance is identical, but == outperforms compare when the strings aren't equal because it does no further processing.

Use compare when you need ordering, == when you only need equality.

As for your choice of construction, it does not matter. == FName(TEXT(...)) is identical to == TEXT(...) (same with compare), the latter case simply implicitly calls the appropriate FName constructor according to the usual C++ conversion rules, given the lack of specific == and Compare overloads. I would go with == TEXT(...) simply because it is less verbose, but also because you'll benefit from any specific potentially-optimized overloads they may choose to add in the future.

more ▼

answered Feb 12 '17 at 06:41 PM

avatar image

46 2 5 9

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question