Just a minor new twist on free()

Mark Brader msb at sq.sq.com
Tue Oct 9 10:07:13 AEST 1990


> While your simple case might indeed not be much of a performance hit,
> what about something like:
>     #define ARRAY_LEN   10000
>     char *array[ARRAY_LEN], **ap;
>     int i;
>     for (ap = array, i = 0; i < ARRAY_LEN; ++i, ++ap)
>     {
>         if (*ap != NULL)
>             free(*ap);
>     }

/* Syntax and type errors corrected - msb */

That's probably still not a significant performance hit.  On the machine
I'm typing this on, it appears to cost somewhat under 0.1 second of CPU.
Even on a slower machine, a real-life program would be *doing* something
with those ten thousand malloc()ed objects, and the cost of ten thousand
compares and branches would probably be negligible in comparison.

Or to put it another way: if you need to execute "if (p) free (p);" so
many times that the "if (p)" tests contribute significantly to the cost
of the program, then you probably need to reorganize the program.

-- 
Mark Brader		"The last 10% of the performance sought contributes
Toronto			 one-third of the cost and two-thirds of the problems."
utzoo!sq!msb, msb at sq.com				-- Norm Augustine

This article is in the public domain.



More information about the Comp.lang.c mailing list