code_junkie

C: Pointers and Arrays Question

Nov 11th, 2011
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. int i;
  2. int a[20];
  3.  
  4. // Init all values to zero
  5. memset(a, 0, sizeof(a));
  6. for (i = 0; i < 20; i++) {
  7. printf("Value of %d is %dn", i, a[i]);
  8. }
  9.  
  10. int i;
  11. int a[20];
  12. int * b;
  13.  
  14. memset(a, 0, sizeof(a));
  15. b = a;
  16. for (i = 0; i < 20; i++) {
  17. printf("Value of %d is %dn", i, *b);
  18. b++;
  19. }
  20.  
  21. _main:
  22. pushl %ebp
  23. movl %esp, %ebp
  24. pushl %edi
  25. pushl %esi
  26. pushl %ebx
  27. subl $108, %esp
  28. call ___i686.get_pc_thunk.bx
  29. "L00000000001$pb":
  30. leal -104(%ebp), %eax
  31. movl $80, 8(%esp)
  32. movl $0, 4(%esp)
  33. movl %eax, (%esp)
  34. call L_memset$stub
  35. xorl %esi, %esi
  36. leal LC0-"L00000000001$pb"(%ebx), %edi
  37. L2:
  38. movl -104(%ebp,%esi,4), %eax
  39. movl %eax, 8(%esp)
  40. movl %esi, 4(%esp)
  41. movl %edi, (%esp)
  42. call L_printf$stub
  43. addl $1, %esi
  44. cmpl $20, %esi
  45. jne L2
  46. addl $108, %esp
  47. popl %ebx
  48. popl %esi
  49. popl %edi
  50. popl %ebp
  51. ret
  52.  
  53. _main:
  54. pushl %ebp
  55. movl %esp, %ebp
  56. pushl %edi
  57. pushl %esi
  58. pushl %ebx
  59. subl $124, %esp
  60. call ___i686.get_pc_thunk.bx
  61. "L00000000001$pb":
  62. leal -104(%ebp), %eax
  63. movl %eax, -108(%ebp)
  64. movl $80, 8(%esp)
  65. movl $0, 4(%esp)
  66. movl %eax, (%esp)
  67. call L_memset$stub
  68. xorl %esi, %esi
  69. leal LC0-"L00000000001$pb"(%ebx), %edi
  70. L2:
  71. movl -108(%ebp), %edx
  72. movl (%edx,%esi,4), %eax
  73. movl %eax, 8(%esp)
  74. movl %esi, 4(%esp)
  75. movl %edi, (%esp)
  76. call L_printf$stub
  77. addl $1, %esi
  78. cmpl $20, %esi
  79. jne L2
  80. addl $124, %esp
  81. popl %ebx
  82. popl %esi
  83. popl %edi
  84. popl %ebp
  85. ret
  86.  
  87. movl -104(%ebp,%esi,4), %eax
  88.  
  89. movl -108(%ebp), %edx
  90. movl (%edx,%esi,4), %eax
  91.  
  92. #include <stdio.h>
  93. #include <stdlib.h>
  94. #include <time.h>
  95. #include <iostream>
  96.  
  97. using namespace std;
  98.  
  99. typedef int64_t int64;
  100. static int64 nsTime() {
  101. struct timespec tp;
  102. clock_gettime(CLOCK_REALTIME, &tp);
  103. return tp.tv_sec*(int64)1000000000 + tp.tv_nsec;
  104. }
  105.  
  106. typedef int T;
  107. size_t const N = 1024*1024*128;
  108. T data[N];
  109.  
  110. int main(int, char**) {
  111. cout << "startingn";
  112.  
  113. {
  114. int64 const a = nsTime();
  115. int sum = 0;
  116. for (size_t i=0; i<N; i++) {
  117. sum += data[i];
  118. }
  119. int64 const b = nsTime();
  120. cout << "Simple loop (indexed): " << (b-a)/1e9 << "n";
  121. }
  122.  
  123. {
  124. int64 const a = nsTime();
  125. int sum = 0;
  126. T *d = data;
  127. for (size_t i=0; i<N; i++) {
  128. sum += *d++;
  129. }
  130. int64 const b = nsTime();
  131. cout << "Simple loop (pointer): " << (b-a)/1e9 << "n";
  132. }
  133.  
  134. {
  135. int64 const a = nsTime();
  136. int sum = 0;
  137. for (size_t i=0; i<N; i++) {
  138. int a = sum+3;
  139. int b = 4-sum;
  140. int c = sum+5;
  141. sum += data[i] + a - b + c;
  142. }
  143. int64 const b = nsTime();
  144. cout << "Loop that uses more ALUs (indexed): " << (b-a)/1e9 << "n";
  145. }
  146.  
  147. {
  148. int64 const a = nsTime();
  149. int sum = 0;
  150. T *d = data;
  151. for (size_t i=0; i<N; i++) {
  152. int a = sum+3;
  153. int b = 4-sum;
  154. int c = sum+5;
  155. sum += *d++ + a - b + c;
  156. }
  157. int64 const b = nsTime();
  158. cout << "Loop that uses more ALUs (pointer): " << (b-a)/1e9 << "n";
  159. }
  160. }
  161.  
  162. char p1[ ] = "12345";
  163. char* p2 = "12345";
  164.  
  165. char *ch = p1[ 3 ]; /* 4 */
  166. ch = *(p2 + 3); /* 4 */
  167.  
  168. char p1[ ] = "12345";
  169. char* p2 = "12345";
  170.  
  171. char *ch = p1[ 3 ]; /* 4 */
  172. ch = *(p2 + 3); /* 4 */
Add Comment
Please, Sign In to add comment