J
John Reye
Hi!
In writing code I often find numerous ways of expressing the same
thing.
I have some questions of "style" and "optimization" that I'd like to
ask.
Example1
========
Variant 1a)
arr.a = f1();
arr.b = f2();
arr.c = f3();
OR
Variant 1b)
struct tmp * const pArr = &arr; // arr + i
pArr->a = f1();
pArr->b = f2();
pArr->c = f3();
Here I probably prefer the 1b). But not on reasons of style, but on
the suspicion (*) that some compilers will produce more efficient code
with the 1b).
(*) - what do you think?
Example2
========
Variant 2a)
arr.a = f1();
arr.b = f2();
arr.c = f3();
arr.d[j++] = f4();
arr.d[j] = f5();
OR
Variant 2b)
struct tmp * const pArr = &arr; // arr + i
pArr->a = f1();
pArr->b = f2();
pArr->c = f3();
pArr->d[j++] = f4();
pArr->d[j] = f5();
OR
Variant 2c)
struct tmp * const pArr = &arr; // arr + i
pArr->a = f1();
pArr->b = f2();
pArr->c = f3();
int * pd = &(pArr->d[j++]);
*pd++ = f4();
*pd = f5();
Here I'd definatley NOT use 2a) because I suspect that double array-
indexing via 2 `[]'-operators is not efficient.
I'd use either 2b) or 2c).
What are you're opinions on this? Which style do you use?
Any tips are highly appreciated. Thanks.
In writing code I often find numerous ways of expressing the same
thing.
I have some questions of "style" and "optimization" that I'd like to
ask.
Example1
========
Variant 1a)
arr.a = f1();
arr.b = f2();
arr.c = f3();
OR
Variant 1b)
struct tmp * const pArr = &arr; // arr + i
pArr->a = f1();
pArr->b = f2();
pArr->c = f3();
Here I probably prefer the 1b). But not on reasons of style, but on
the suspicion (*) that some compilers will produce more efficient code
with the 1b).
(*) - what do you think?
Example2
========
Variant 2a)
arr.a = f1();
arr.b = f2();
arr.c = f3();
arr.d[j++] = f4();
arr.d[j] = f5();
OR
Variant 2b)
struct tmp * const pArr = &arr; // arr + i
pArr->a = f1();
pArr->b = f2();
pArr->c = f3();
pArr->d[j++] = f4();
pArr->d[j] = f5();
OR
Variant 2c)
struct tmp * const pArr = &arr; // arr + i
pArr->a = f1();
pArr->b = f2();
pArr->c = f3();
int * pd = &(pArr->d[j++]);
*pd++ = f4();
*pd = f5();
Here I'd definatley NOT use 2a) because I suspect that double array-
indexing via 2 `[]'-operators is not efficient.
I'd use either 2b) or 2c).
What are you're opinions on this? Which style do you use?
Any tips are highly appreciated. Thanks.