daily pastebin goal
29%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int main(){
  2.         int* ptr;
  3.         int a;
  4.         register int b;
  5.         ptr = &a;
  6.         ptr = &b; //this won't compile
  7.         return 0;
  8. }
  9.    
  10. /* reg_or_not.c */  
  11.  
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include <stdlib> //not requiered for Linux
  15. #define LAPSb 50
  16. #define LAPS 50000
  17. #define MAXb 50
  18. #define MAX 50000
  19.  
  20.  
  21. int main (void)
  22. {
  23. /* 20 ints and 2 register ints */  
  24.  
  25. register int k,l;
  26. int a,aa,b,bb,c,cc,d,dd,e,ee,f,ff,g,gg,h,hh,i,ii,j,jj;
  27.  
  28.  
  29. /* measure some ticks also */  
  30.  
  31. clock_t start_1,start_2;
  32. clock_t finish_1,finish_2;
  33. long tmp; //just for the workload
  34.  
  35.  
  36. /* pointer declarations of all ints */
  37.  
  38. int *ap, *aap, *bp, *bbp, *cp, *ccp, *dp, *ddp, *ep, *eep;
  39. int *fp, *ffp, *gp, *ggp, *hp, *hhp, *ip, *iip, *jp, *jjp;
  40. int *kp,*lp;
  41.  
  42. /* end of declarations */
  43. /* read memory addresses, if possible - which can't be done in a CPU-register */    
  44.  
  45. ap=&a; aap=&aa; bp=&b; bbp=&bb;
  46. cp=&c; ccp=&cc; dp=&d; ddp=&dd;
  47. ep=&e; eep=&ee; fp=&f; ffp=&ff;
  48. gp=&g; ggp=&gg; hp=&h; hhp=&hh;
  49. ip=&i; iip=&ii; jp=&j; jjp=&jj;
  50.  
  51. //kp=&k;  //won't compile if k is stored in a CPU register  
  52. //lp=&l;  //same - but try both ways !
  53.  
  54.  
  55. /* what address , isn't the issue in this case - but if stored in memory    some "crazy" number will be shown, whilst CPU-registers can't be read */
  56.  
  57. printf("Address a aa: %u     %un",a,aa);
  58. printf("Address b bb: %u     %un",b,bb);
  59. printf("Address c cc: %u     %un",c,cc);
  60. printf("Address d dd: %u     %un",d,dd);
  61. printf("Address e ee: %u     %un",e,ee);
  62. printf("Address f ff: %u     %un",f,ff);
  63. printf("Address g gg: %u     %un",g,gg);
  64. printf("Address h hh: %u     %un",h,hh);
  65. printf("Address i ii: %u     %un",i,ii);
  66. printf("Address j jj: %u     %unn",j,jj);
  67.  
  68. //printf("Address k:  %u n",k); //no reason to try "k" actually is in a CPU-register
  69. //printf("Address l:  %u n",l);
  70.  
  71.  
  72. start_2=clock(); //just for fun      
  73.  
  74. /* to ensure workload */
  75. for (a=1;a<LAPSb;a++) {for (aa=0;aa<MAXb;aa++);{tmp+=aa/a;}}
  76. for (b=1;b<LAPSb;b++) {for (bb=0;bb<MAXb;bb++);{tmp+=aa/a;}}
  77. for (a=1;c<LAPSb;c++) {for (cc=0;cc<MAXb;cc++);{tmp+=bb/b;}}
  78. for (d=1;d<LAPSb;d++) {for (dd=0;dd<MAXb;dd++);{tmp+=cc/c;}}
  79. for (e=1;e<LAPSb;e++) {for (ee=0;ee<MAXb;ee++);{tmp+=dd/d;}}
  80. for (f=1;f<LAPSb;f++) {for (ff=0;ff<MAXb;ff++);{tmp+=ee/e;}}
  81. for (g=1;g<LAPSb;g++) {for (gg=0;gg<MAXb;gg++);{tmp+=ff/f;}}
  82. for (h=1;h<LAPSb;h++) {for (hh=0;hh<MAXb;hh++);{tmp+=hh/h;}}
  83. for (jj=1;jj<LAPSb;jj++) {for (ii=0;ii<MAXb;ii++);{tmp+=ii/jj;}}
  84.  
  85. start_1=clock(); //see following printf
  86. for (i=0;i<LAPS;i++) {for (j=0;j<MAX;j++);{tmp+=j/i;}} /* same double   loop - in supposed memory */
  87. finish_1=clock(); //see following printf
  88.  
  89. printf ("Memory: %ld ticksnn", finish_1 - start_1); //ticks for memory
  90.  
  91. start_1=clock(); //see following printf
  92. for (k=0;k<LAPS;k++) {for (l=0;l<MAX;l++);{tmp+=l/k;}}  /* same double       loop - in supposed register*/
  93. finish_1=clock(); //see following printf    
  94.  
  95. printf ("Register: %ld ticksnn", finish_1 - start_1); //ticks for CPU register (?) any difference ?  
  96.  
  97. finish_2=clock();
  98.  
  99. printf ("Total: %ld ticksnn", finish_2 - start_2); //really for fun only          
  100.  
  101. system("PAUSE"); //only requiered for Windows, so the CMD-window doesn't vanish    
  102.  
  103. return 0;
  104.  
  105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top