Reputation: 761
Strings are fixed length 8, contain alphanumeric characters and right-padded with spaces.
I.e,
"STRING1 "
"STR2 "
"S "
etc..
I was thinking memcmp
might be the fastest here?
Upvotes: 1
Views: 705
Reputation: 43508
If you ensure that the strings are aligned on an 8-byte boundary via compiler-specific attributes, you can do:
uint64_t a = *((uint64_t *) "STRING1 ");
uint64_t b = *((uint64_t *) "STR2 ");
Then a == b
should yield to a single 64-bit instruction.
Or, if they are just constant immutable strings (stored in a read-only area of the process), you can go on with comparing the const char *
pointers themselves. It's still a reliable test since a string literal that appears twice in the current translation unit should refer to the same memory:
/* fails because the two strings are stored at different locations */
"STRING1 " == "STR2 "
/* should succeed, even the silliest compiler should merge both literals */
"STRING1 " == "STRING1 "
Upvotes: 10
Reputation: 81349
If the strings have fixed equal length, then memcmp
is a good approach.
Upvotes: 4