Appendix 3: Cost Models for Time and Space

Section 7.2 describes two little programs for estimating the time and space consumed by various primitive operations. This appendix shows how those can grow into useful programs for generating one-page time and space estimates. The complete source code for both programs can be found at this book’s web site.

The program spacemod.cpp produces a model of the space consumed by various constructs in C++. The first part of the program uses a sequence of statements like

cout << "sizeof(char)=" << sizeof(char);cout << " sizeof(short)=" << sizeof(short);

to give precise measurements of primitive objects:

sizeof(char)=l  sizeof(short)=2  sizeof(int)=4 ...

Get Programming Pearls, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.