endianess detection and performance in C
问题 I've got a performance critical C code that needs to work on a variety of platform. Some of them are little endian, others are big endian. Detecting endianess is currently an imperfect process, based on macro detection. But it's difficult to be sure that the macro detection will work for all combinations of systems and compilers. Welcome to the world of portable code. One relatively safe way to detect endianess is to use a runtime test, and hope that it will get optimized out by the compiler.