Originally Posted by
whiteflags
I disagree that it runs perfectly. Why is iSum different from the last line and which total is correct? Your sample output raises more debugging concerns.
oO...what the...i didnt notice that. beats me...i dont see why that should happen...the OP should be more scared than you or I though....
And in fact doing so may slow your code down.
hmmm....i dont know
heres a pretty simple test...
Code:
#include<stdio.h>
int main()
{
register int i=1;
while(i>0)
{
i++;
}
printf("%d",i);
}
i measured the time it took ... 3 times
Code:
[c_d@localhost C]$ gcc product.c
[c_d@localhost C]$ time ./a.out
-2147483648
real 0m8.141s
user 0m8.121s
sys 0m0.002s
[c_d@localhost C]$ time ./a.out
-2147483648
real 0m8.116s
user 0m8.108s
sys 0m0.001s
[c_d@localhost C]$ time ./a.out
-2147483648
real 0m8.121s
user 0m8.106s
sys 0m0.000s
average(register variable)
realtime=8.126
usertime=8.111666667
systime=0.001
then i changed the program to
[code]
Code:
#include<stdio.h>
int main()
{
int i=1;
while(i>0)
{
i++;
}
printf("%d",i);
}
the time taken were
Code:
[c_d@localhost C]$ gcc product.c
[c_d@localhost C]$ time ./a.out
-2147483648
real 0m8.152s
user 0m8.126s
sys 0m0.004s
[c_d@localhost C]$ time ./a.out
-2147483648
real 0m8.167s
user 0m8.132s
sys 0m0.001s
[c_d@localhost C]$ time ./a.out
-2147483648
real 0m8.151s
user 0m8.127s
sys 0m0.002s
average:(normal int)
realtime=8.156666667
usertime=8.128333333
systime=0.002333333
then i modified to
Code:
#include<stdio.h>
int main()
{
static int i=1;
while(i>0)
{
i++;
}
printf("%d",i);
}
and the results were
Code:
[c_d@localhost C]$ gcc product.c
[c_d@localhost C]$ time ./a.out
-2147483648
real 0m8.373s
user 0m8.362s
sys 0m0.000s
[c_d@localhost C]$ time ./a.out
-2147483648
real 0m8.128s
user 0m8.122s
sys 0m0.001s
[c_d@localhost C]$ time ./a.out
-2147483648
real 0m8.690s
user 0m8.667s
sys 0m0.003s
average(static int)
realtime=8.397
usertime=8.383666667
systime=0.001333333
according to this little experiment...
performance of register int >int> static int
I tried using the register keyword several times cos I thought it would make things faster... It always turned out slower than ordinary variables. static variables generally seem to speed things up, but apparently they cripple the optimiser or something. I still use them now and again in functions that get called very often.
hmm...i heard that static should be used in functions that are called many times, like recursive functions...because...every time , instead of the variables initialising again and again , static would mean one time initialisation, which would save processing time...