Efficient coding considered harmful?

Steven Ryan smryan at garth.UUCP
Wed Nov 2 10:21:51 AEST 1988


>>Realloc has to copy data, so this should be less efficient than just
>>doing another malloc & free.
>
>Err, umm, "realloc" is often used when you have e.g. an array with N

ERRR, UMMMM, we seem to have lost the point here.

It sounds as though someone's guessing what the data structure size should
be, guesses wrong, and has to increase it. If data structure size keeps
getting bumped bit by bit until the problem size is finally determined,
then we've got an O(n) structure which has been copied O(n/increment)=O(n)
times so that the cost of creating just this structure is O(n**2) so that
the overall time is quadratic, at best.

This sounds more like someone's using an inappropriate data structure which
is going to cost far more than any amount tweaking is going to get you.

Tweaking a program gives at best linear improvement.

Fixing the basic algorithm gives at least linear improvement.

--------------------------------------------------------------------------------
Well, gee, how do you allocate an indefinite sized array?

Glad you asked. I stuff it down a stack, each element individually
handcrafted with malloc, until I've reached the end and know exactly
how big it is. Then, IF I need random access, I copy it to an array.
Linear time.



More information about the Comp.lang.c mailing list