Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 8.74 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Why am I observing multiple inheritance to be faster than single?
  2. #include <iostream>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. unsigned long a=0;
  8.  
  9. class A {
  10.   public:
  11.     virtual int f() __attribute__ ((noinline)) { return a; }
  12. };
  13.  
  14. class B : public A {                                                                              
  15.   public:                                                                                                                                                                        
  16.     virtual int f() __attribute__ ((noinline)) { return a; }                                      
  17.     void g() __attribute__ ((noinline)) { return; }                                              
  18. };                                                                                                
  19.  
  20. int main() {                                                                                      
  21.   cin>>a;                                                                                        
  22.   A* obj;                                                                                        
  23.   if (a>3)                                                                                        
  24.     obj = new B();
  25.   else
  26.     obj = new A();                                                                                
  27.  
  28.   unsigned long result=0;                                                                        
  29.  
  30.   for (int i=0; i<65535; i++) {                                                                  
  31.     for (int j=0; j<65535; j++) {                                                                
  32.       result+=obj->f();                                                                          
  33.     }                                                                                            
  34.   }                                                                                              
  35.  
  36.   cout<<result<<"n";                                                                            
  37. }
  38.        
  39. #include <iostream>
  40. #include <stdlib.h>
  41.  
  42. using namespace std;
  43.  
  44. unsigned long a=0;
  45.  
  46. class A {
  47.   public:
  48.     virtual int f() __attribute__ ((noinline)) { return a; }
  49. };
  50.  
  51. class dummy {
  52.   public:
  53.     virtual void g() __attribute__ ((noinline)) { return; }
  54. };
  55.  
  56. class B : public A, public dummy {
  57.   public:
  58.     virtual int f() __attribute__ ((noinline)) { return a; }
  59.     virtual void g() __attribute__ ((noinline)) { return; }
  60. };
  61.  
  62.  
  63. int main() {
  64.   cin>>a;
  65.   A* obj;
  66.   if (a>3)
  67.     obj = new B();
  68.   else
  69.     obj = new A();
  70.  
  71.   unsigned long result=0;
  72.  
  73.   for (int i=0; i<65535; i++) {
  74.     for (int j=0; j<65535; j++) {
  75.       result+=obj->f();
  76.     }
  77.   }
  78.  
  79.   cout<<result<<"n";
  80. }
  81.        
  82. real    0m8.635s
  83. user    0m8.608s
  84. sys 0m0.003s
  85.        
  86. real    0m10.072s
  87. user    0m10.045s
  88. sys 0m0.001s
  89.        
  90. class B : public dummy, public A {
  91.        
  92. real    0m11.516s
  93. user    0m11.479s
  94. sys 0m0.002s
  95.        
  96. cout << "vtable: " << *(void**)obj << endl;
  97.        
  98. #include <iostream>
  99. #include <stdlib.h>
  100. #include <time.h>
  101. using namespace std;
  102. unsigned long a=0;
  103.  
  104.  
  105. #ifdef SINGLE
  106. class A {
  107.   public:
  108.     virtual int f() { return a; }
  109. };
  110.  
  111. class B : public A {
  112.   public:
  113.     virtual int f() { return a; }                                      
  114.     void g() { return; }                                              
  115. };      
  116. #endif
  117.  
  118. #ifdef MULTIPLE
  119. class A {
  120.   public:
  121.     virtual int f() { return a; }
  122. };
  123.  
  124. class dummy {
  125.   public:
  126.     virtual void g() { return; }
  127. };
  128.  
  129. class B : public A, public dummy {
  130.   public:
  131.     virtual int f() { return a; }
  132.     virtual void g() { return; }
  133. };
  134. #endif
  135.  
  136. int main() {
  137.     cin >> a;
  138.     A* obj;
  139.     if (a > 3)
  140.         obj = new B();
  141.     else
  142.         obj = new A();
  143.  
  144.     unsigned long result = 0;
  145.  
  146.  
  147.     clock_t time0 = clock();
  148.  
  149.     for (int i=0; i<65535; i++) {
  150.         for (int j=0; j<65535; j++) {
  151.             result += obj->f();
  152.         }
  153.     }      
  154.  
  155.     clock_t time1 = clock();  
  156.     cout << (double)(time1 - time0) / CLOCKS_PER_SEC << endl;    
  157.  
  158.     cout << result << "n";
  159.     system("pause");  //  This is useless in Linux, but I left it here for a reason.
  160. }
  161.        
  162. .L5:
  163.     call    clock
  164.     movl    $65535, %r13d
  165.     movq    %rax, %r14
  166.     xorl    %r12d, %r12d
  167.     .p2align 4,,10
  168.     .p2align 3
  169. .L6:
  170.     movl    $65535, %ebx
  171.     .p2align 4,,10
  172.     .p2align 3
  173. .L7:
  174.     movq    0(%rbp), %rax
  175.     movq    %rbp, %rdi
  176.     call    *(%rax)
  177.     cltq
  178.     addq    %rax, %r12
  179.     subl    $1, %ebx
  180.     jne .L7
  181.     subl    $1, %r13d
  182.     jne .L6
  183.     call    clock
  184.        
  185. ; %rbp = vtable
  186.  
  187. movq    0(%rbp), %rax   ; Dereference function pointer from vtable
  188. movq    %rbp, %rdi
  189. call    *(%rax)         ; Call function pointer - f()
  190.        
  191. #ifdef SINGLE
  192. class A {
  193.   public:
  194.     virtual int f() { return a; }
  195. };
  196.  
  197. class B : public A {
  198.   public:
  199.     virtual int f() { return a; }                                      
  200.     void g() { return; }                                              
  201. };      
  202. #endif
  203.  
  204. #ifdef MULTIPLE
  205. class A {
  206.   public:
  207.     virtual int f() { return a; }
  208. };
  209.  
  210. class dummy {
  211.   public:
  212.     virtual void g() { return; }
  213. };
  214.  
  215. class B : public A, public dummy {
  216.   public:
  217.     virtual int f() { return a; }
  218.     virtual void g() { return; }
  219. };
  220. #endif
  221.  
  222. int main() {
  223.     cin >> a;
  224.     A* obj;
  225.     if (a > 3)
  226.         obj = new B();
  227.     else
  228.         obj = new A();
  229.  
  230.     unsigned long result = 0;
  231.  
  232.  
  233.     clock_t time0 = clock();
  234.  
  235.     for (int i=0; i<65535; i++) {
  236.         for (int j=0; j<65535; j++) {
  237.             result += obj->f();
  238.         }
  239.     }      
  240.  
  241.     clock_t time1 = clock();  
  242.     cout << (double)(time1 - time0) / CLOCKS_PER_SEC << endl;    
  243.  
  244.     cout << result << "n";
  245. //    system("pause");
  246. }
  247.        
  248. cout << "vtable: " << *(void**)obj << endl;
  249.  
  250. for (int i=0; i<65535; i++) {
  251.   for (int j=0; j<65535; j++) {
  252.     result+=obj->f();
  253.   }
  254. }
  255.        
  256. 40092d:       bb fe ff 00 00          mov    $0xfffe,%ebx                                      
  257. 400932:       48 8b 45 00             mov    0x0(%rbp),%rax                                    
  258. 400936:       48 89 ef                mov    %rbp,%rdi                                          
  259. 400939:       ff 10                   callq  *(%rax)                                            
  260. 40093b:       48 98                   cltq                                                      
  261. 40093d:       49 01 c4                add    %rax,%r12                                          
  262. 400940:       ff cb                   dec    %ebx                                              
  263. 400942:       79 ee                   jns    400932 <main+0x42>                                
  264. 400944:       41 ff c5                inc    %r13d                                              
  265. 400947:       41 81 fd fe ff 00 00    cmp    $0xfffe,%r13d                                      
  266. 40094e:       7e dd                   jle    40092d <main+0x3d>
  267.        
  268. for (int i=0; i<65535; i++) {
  269.   for (int j=0; j<65535; j++) {
  270.     result+=obj->f();
  271.   }
  272. }
  273.        
  274. 400a54:       bb fe ff 00 00          mov    $0xfffe,%ebx
  275. 400a59:       66                      data16                                                    
  276. 400a5a:       66                      data16
  277. 400a5b:       66                      data16                                                    
  278. 400a5c:       90                      nop                                                      
  279. 400a5d:       66                      data16                                                    
  280. 400a5e:       66                      data16                                                    
  281. 400a5f:       90                      nop                                                      
  282. 400a60:       48 8b 45 00             mov    0x0(%rbp),%rax                                    
  283. 400a64:       48 89 ef                mov    %rbp,%rdi                                          
  284. 400a67:       ff 10                   callq  *(%rax)
  285. 400a69:       48 98                   cltq  
  286. 400a6b:       49 01 c4                add    %rax,%r12                                          
  287. 400a6e:       ff cb                   dec    %ebx                                              
  288. 400a70:       79 ee                   jns    400a60 <main+0x70>                                
  289. 400a72:       41 ff c5                inc    %r13d                                              
  290. 400a75:       41 81 fd fe ff 00 00    cmp    $0xfffe,%r13d
  291. 400a7c:       7e d6                   jle    400a54 <main+0x64>
  292.        
  293. .L30:
  294.         movl    $65534, %ebx
  295.         .p2align 4,,7                  
  296. .L29:
  297.         movq    (%rbp), %rax            
  298.         movq    %rbp, %rdi
  299.         call    *(%rax)
  300.         cltq    
  301.         addq    %rax, %r12                                                                        
  302.         decl    %ebx
  303.         jns     .L29
  304.         incl    %r13d
  305.         cmpl    $65534, %r13d
  306.         jle     .L30
  307.        
  308. 0x400a59 = 0b101001011001
  309.        
  310. 0x400932 = 0b100100110010
  311.        
  312. if (a>3) {
  313.     B* objb = new B();
  314.     objb->a = 5;
  315.     obj = objb;
  316. }
  317. else
  318.     obj = new A();