Sunday 20 November 2011

Does the use of white space affect program speed, executable size, or efficiency? in C programming

Does the use of white space affect program speed, executable size, or efficiency?

No. As with comments, all white space is ignored by the compiler. When your program is compiled, all white
space and comments are ignored, and only the executable statements are parsed and eventually put into the
final compiled version of the program.

The use of white space in your C programs can help make your programs more readable and improve clarity
by separating out your executable statements, functions, comments, and so forth. Many times, you improve your program’s readability by simply adding blank lines between statements. For instance, consider the following portion of code:

/* clcpy by GBlansten */
void clcpy(EMP* e, int rh, int ot)
{ e->grspy=(e->rt*rh)+(e->rt*ot*1.5);
e->txamt=e->grspy*e->txrt;
e->ntpy=e->grspy-e->txamt;
updacctdata(e);
if (e->dd==false) cutpyck(e);
else prtstb(e); }

As you can see, this function is quite a mess. Sure, it works, but no programmer in the world would like to maintain this type of code. Consider what the function would look like if you were to apply some of the naming conventions used in this chapter (such as using underscores and eliminating short cryptic names), use some bracing techniques (such as Allman’s technique), and add some white space and comments:

/**********************************************************************
Function Name: calc_pay
Parameters: emp - EMPLOYEE pointer that points to employee data
reg_hours - The number of regular hours (<= 40) employee
has worked
ot_hours - The number of overtime hours (> 40) employee
has worked
Author: Gern Blansten
Date Written: 13 dec 1993
Modifications: 04 sep 1994 by Lloyd E. Work
- Rewrote function to make it readable by human beings.
Description: This function calculates an employee’s gross pay, tax
amount, and net pay, and either prints a paycheck for the
employee or (in the case of those who have direct deposit)
prints a paycheck stub.
**********************************************************************/
void calc_pay(EMPLOYEE* emp, int reg_hours, int ot_hours)
{
/* gross pay = (employee rate * regular hours) +
(employee rate * overtime hours * 1.5) */
emp->gross_pay = (emp->rate * reg_hours) +
(emp->rate * ot_hours * 1.5);
/* tax amount = gross pay * employee’s tax rate */
emp->tax_amount = emp->gross_pay * emp->tax_rate;
/* net pay = gross pay - tax amount */
emp->net_pay = emp->gross_pay - emp->tax_amount;
/* update the accounting data */
update_accounting_data(emp);
/* check for direct deposit */
if (emp->direct_deposit == false)
cut_paycheck(emp); /* print a paycheck */
else
print_paystub(emp); /* print a paycheck stub */
}

As you can see, Lloyd’s version (the one with liberal use of comments, white space, descriptive variable names, and so on) is much more readable than Gern’s ill-fated version. Chances are that good ’ol Gern has been (or soon will be) replaced....

You should use white space (and comments, for that matter) as much as you see fit. Doing so will help your
programs to be much more readable—and possibly lengthen your job expectancy.

Cross Reference:

XIX.3: Does the use of comments affect program speed, executable size, or efficiency?
XIX.6: Do longer variable names affect the speed, executable size, or efficiency of a program?

No comments:

Post a Comment