Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main(){
- int* ptr;
- int a;
- register int b;
- ptr = &a;
- ptr = &b; //this won't compile
- return 0;
- }
- /* reg_or_not.c */
- #include <stdio.h>
- #include <time.h>
- #include <stdlib> //not requiered for Linux
- #define LAPSb 50
- #define LAPS 50000
- #define MAXb 50
- #define MAX 50000
- int main (void)
- {
- /* 20 ints and 2 register ints */
- register int k,l;
- int a,aa,b,bb,c,cc,d,dd,e,ee,f,ff,g,gg,h,hh,i,ii,j,jj;
- /* measure some ticks also */
- clock_t start_1,start_2;
- clock_t finish_1,finish_2;
- long tmp; //just for the workload
- /* pointer declarations of all ints */
- int *ap, *aap, *bp, *bbp, *cp, *ccp, *dp, *ddp, *ep, *eep;
- int *fp, *ffp, *gp, *ggp, *hp, *hhp, *ip, *iip, *jp, *jjp;
- int *kp,*lp;
- /* end of declarations */
- /* read memory addresses, if possible - which can't be done in a CPU-register */
- ap=&a; aap=&aa; bp=&b; bbp=&bb;
- cp=&c; ccp=&cc; dp=&d; ddp=ⅆ
- ep=&e; eep=ⅇ fp=&f; ffp=&ff;
- gp=&g; ggp=≫ hp=&h; hhp=&hh;
- ip=&i; iip=ⅈ jp=&j; jjp=&jj;
- //kp=&k; //won't compile if k is stored in a CPU register
- //lp=&l; //same - but try both ways !
- /* 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 */
- printf("Address a aa: %u %un",a,aa);
- printf("Address b bb: %u %un",b,bb);
- printf("Address c cc: %u %un",c,cc);
- printf("Address d dd: %u %un",d,dd);
- printf("Address e ee: %u %un",e,ee);
- printf("Address f ff: %u %un",f,ff);
- printf("Address g gg: %u %un",g,gg);
- printf("Address h hh: %u %un",h,hh);
- printf("Address i ii: %u %un",i,ii);
- printf("Address j jj: %u %unn",j,jj);
- //printf("Address k: %u n",k); //no reason to try "k" actually is in a CPU-register
- //printf("Address l: %u n",l);
- start_2=clock(); //just for fun
- /* to ensure workload */
- for (a=1;a<LAPSb;a++) {for (aa=0;aa<MAXb;aa++);{tmp+=aa/a;}}
- for (b=1;b<LAPSb;b++) {for (bb=0;bb<MAXb;bb++);{tmp+=aa/a;}}
- for (a=1;c<LAPSb;c++) {for (cc=0;cc<MAXb;cc++);{tmp+=bb/b;}}
- for (d=1;d<LAPSb;d++) {for (dd=0;dd<MAXb;dd++);{tmp+=cc/c;}}
- for (e=1;e<LAPSb;e++) {for (ee=0;ee<MAXb;ee++);{tmp+=dd/d;}}
- for (f=1;f<LAPSb;f++) {for (ff=0;ff<MAXb;ff++);{tmp+=ee/e;}}
- for (g=1;g<LAPSb;g++) {for (gg=0;gg<MAXb;gg++);{tmp+=ff/f;}}
- for (h=1;h<LAPSb;h++) {for (hh=0;hh<MAXb;hh++);{tmp+=hh/h;}}
- for (jj=1;jj<LAPSb;jj++) {for (ii=0;ii<MAXb;ii++);{tmp+=ii/jj;}}
- start_1=clock(); //see following printf
- for (i=0;i<LAPS;i++) {for (j=0;j<MAX;j++);{tmp+=j/i;}} /* same double loop - in supposed memory */
- finish_1=clock(); //see following printf
- printf ("Memory: %ld ticksnn", finish_1 - start_1); //ticks for memory
- start_1=clock(); //see following printf
- for (k=0;k<LAPS;k++) {for (l=0;l<MAX;l++);{tmp+=l/k;}} /* same double loop - in supposed register*/
- finish_1=clock(); //see following printf
- printf ("Register: %ld ticksnn", finish_1 - start_1); //ticks for CPU register (?) any difference ?
- finish_2=clock();
- printf ("Total: %ld ticksnn", finish_2 - start_2); //really for fun only
- system("PAUSE"); //only requiered for Windows, so the CMD-window doesn't vanish
- return 0;
- }
Add Comment
Please, Sign In to add comment