Use the
int data type instead of
char when
performing arithmetic operations.
char_var += '0';
int_var += '0'; /* better */
A
char type variable is efficient when you are:
- Assigning a literal to a char variable
- Comparing the variable with a char literal
For example:
char_var = 27;
if (char_var == 'D')
Table 1 lists analogous data types and shows
which data types are more expensive to reference.
Table 1. Referencing data typesMore Expensive |
Less Expensive |
unsigned short |
signed short (Although unsigned short is less expensive on
many systems, the z/OS® implementation of signed short is less expensive.) |
signed char |
unsigned char |
long double |
double |
Longer decimal |
Shorter decimal |
For storage efficiency, the compiler packs enumeration variables
in 1, 2 or 4 bytes, depending on the largest value of a constant.
When performance is critical, expand the size to a fullword either
by adding an enumeration constant with a large value or by specifying
the ENUMSIZE compiler option. For example:
enum byte { land, sea, air, space };
enum word { low, medium, high, expand_to_fullword = INT_MAX };
Example that is equivalent to using the ENUMSIZE(INT) compiler
option:
enum word { low, medium, high };
Fullword enumeration variables are preferred as function parameters.
For efficient use of
extern variables:
- Place scalars ahead of arrays in extern struct.
- Copy heavily referenced scalars to auto or register variables
(especially in a loop).
When using
float:
- When passing variables of type float to a function, an implicit
widening to double occurs (which takes time).
- On some machines divisions of type float are faster than those
of type double.
When using bit fields, be aware that:
- Even though the compiler supports a bit field spanning more than
4 bytes, the cost of referencing it is higher.
- An unsigned bit field is preferred over a signed bit
field.
- A bit field used to store integer values should have a length
of 8, 16, or 24 bits and be on a byte boundary.
struct { unsigned xval :8,
xbool :1,
xmany :6,
xset :1;
} b;
if (b.xval == 3)
⋮
if (b.xmany + 5 == x) /* inefficient because it does not */
/* fall on a byte boundary */
⋮
if (b.xbool)
⋮