1

Suppose that my machine word is 32 bits long, and I have to sort 8-character strings.
I've read that if you pack the characters into words, the comparisons will be faster since you use aligned memory access.
So in our case, we will divide the strings into two words of 4 bytes, and use the first word of each string to compare, if they happen to be equal, then you check the second word of each string.

Does performance improve? Does it get any faster? Since coding gets more complex, does it pay off? And if it works, does it work for all languages?

2
  • I don't think you will know before you actually try. Compiler optimization nowadays get more and more sophisticated, so just try.
    – ZhangChn
    Jun 15, 2011 at 19:14
  • Your right, I haven't thought on that, ha. I'll try that tonight. Jun 15, 2011 at 19:20

1 Answer 1

1

The answer is architecture dependent, and compilers usually do a pretty good job on optimizing these things. Unless you are programming for a specific architecture, needs to squeeze out every bit of speed and really know the processor, you'd better let the compiler decide the alignment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.