I want to use this to compare short strings(4 to 8 chars long) in performance critical code. I was thinking to cast them to integer values and compare those values instead of comparing the strings:
const char* str = "abcdefgh";
uint64_t num = *reinterpret_cast<const uint64_t*>(str);
Is it safe to cast str
to uint64_t
without checking alignment of the char*
pointer? I'm using the code only on ARM and Intel CPUs, 32 and 64 bits.
If the behaviour is well defined and the cast is safe, should I expect performance degradation when the pointer is not aligned to 8 bytes?
Do you have any other suggestions to do this in a very fast way?