C++ Performance: Two shorts vs int bit operations -


what of options have best performance: use 2 shorts 2 sensible informations, or use 1 int , use bit operations retrive half of each sensible information?

this may vary depending on architecture , compiler, 1 using int , bit operations on have less performance. difference of performance minimal till haven't written code require level of optimization. depend on compiler these kind of optimizations me.

now let check below c++ code simulates behaviur:

int main() {     int x = 100;     short = 255;     short b = 127;     short p = x >> 16;     short q =  x & 0xffff;     short y =  a;     short z = b;      return 0; } 

the corresponding assembly code on x86_64 system (from gnu g++) shown below:

00000000004004ed <main>: int main() {   4004ed:   55                      push   %rbp   4004ee:   48 89 e5                mov    %rsp,%rbp     int x = 100;   4004f1:   c7 45 fc 64 00 00 00    movl   $0x64,-0x4(%rbp)     short = 255;   4004f8:   66 c7 45 f0 ff 00       movw   $0xff,-0x10(%rbp)     short b = 127;   4004fe:   66 c7 45 f2 7f 00       movw   $0x7f,-0xe(%rbp)     short p = x >> 16;   400504:   8b 45 fc                mov    -0x4(%rbp),%eax   400507:   c1 f8 10                sar    $0x10,%eax   40050a:   66 89 45 f4             mov    %ax,-0xc(%rbp)     short q =  x & 0xffff;   40050e:   8b 45 fc                mov    -0x4(%rbp),%eax   400511:   66 89 45 f6             mov    %ax,-0xa(%rbp)     short y =  a;   400515:   0f b7 45 f0             movzwl -0x10(%rbp),%eax   400519:   66 89 45 f8             mov    %ax,-0x8(%rbp)     short z = b;   40051d:   0f b7 45 f2             movzwl -0xe(%rbp),%eax   400521:   66 89 45 fa             mov    %ax,-0x6(%rbp)      return 0;   400525:   b8 00 00 00 00          mov    $0x0,%eax } 

as see, "short p = x >> 16" slowest uses expensive right shift operation. while other assignments equal in terms of cost.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -