Um festzustellen, ob das Schlüsselwort register eine Bedeutung hat, reichen winzige Beispielcodes nicht aus. Hier ist ein C-Code, der mir vorschlägt, dass das Schlüsselwort register immer noch eine Bedeutung hat. Aber es könnte bei GCC unter Linux anders sein, ich weiß es nicht. Wird das Register int k & l in einem CPU-Register gespeichert oder nicht? Linux-Benutzer sollten (insbesondere) mit GCC und Optimierung kompilieren. Bei Borland bcc32 scheint das Schlüsselwort register zu funktionieren (in diesem Beispiel), da der & -operator Fehlercodes für deklarierte Ganzzahlen des Registers angibt. HINWEIS! Dies ist bei einem winzigen Beispiel mit Borland unter Windows NICHT der Fall! Um wirklich zu sehen, was der Compiler optimiert oder nicht, muss es ein mehr als winziges Beispiel sein. Leere Schleifen reichen nicht aus! Trotzdem - WENN eine Adresse mit dem & -operator gelesen werden kann, wird die Variable nicht in einem CPU-Register gespeichert. Aber wenn eine vom Register deklarierte Variable nicht gelesen werden kann (was beim Kompilieren zu Fehlercode führt) - muss ich davon ausgehen, dass das Schlüsselwort register die Variable tatsächlich in ein CPU-Register einfügt. Es kann auf verschiedenen Plattformen unterschiedlich sein, ich weiß es nicht. (Wenn es funktioniert, ist die Anzahl der "Ticks" mit der Registerdeklaration weitaus geringer.
/* 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 %u\n",a,aa);
printf("Address b bb: %u %u\n",b,bb);
printf("Address c cc: %u %u\n",c,cc);
printf("Address d dd: %u %u\n",d,dd);
printf("Address e ee: %u %u\n",e,ee);
printf("Address f ff: %u %u\n",f,ff);
printf("Address g gg: %u %u\n",g,gg);
printf("Address h hh: %u %u\n",h,hh);
printf("Address i ii: %u %u\n",i,ii);
printf("Address j jj: %u %u\n\n",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 ticks\n\n", 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 ticks\n\n", finish_1 - start_1); //ticks for CPU register (?) any difference ?
finish_2=clock();
printf ("Total: %ld ticks\n\n", finish_2 - start_2); //really for fun only
system("PAUSE"); //only requiered for Windows, so the CMD-window doesn't vanish
return 0;
}