O'Reilly logo

Programming Pearls, Second Edition by Jon Bentley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required