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, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.