简体   繁体   中英

avoiding array index promotion in hopes of better performance

I have a huge array of integers and those integers are not greater than 0xFFFF . Therefore I would like save some space and store them as unsigned short .

unsigned short addresses[50000 /* big number over here */];

Later I would use this array as follows

data[addresses[i]];

When I use only 2 bytes to store my integers, they are being promoted to either 4 or 8 bytes (depending on architecture) when used as array indices. Speed is very important to me, therefore should I rather save my integers as unsigned int to avoid wasting time on type promotion? This array may get absolutely massive and I would also like to save some space, but not at the cost of performance. What should I do?

Anything to do with C style arrays usually gets compiled in to machine instructions that use the memory addressing of the architecture for which you compile, thus trying to save space on array indexes will not work.

If anything, you might break whatever optimizations your compiler might want to implement.

5 Million integer values, even on a 64bit machine, comes to about 40 MB RAM.

While I am sure your code does other things, this is not that much memory to sacrifice performance.

Since you chose to keep all those values in RAM in the first place, presumably for speed, don't ruin it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM