Aussie AI

Measuring Code Size and Static Storage

  • Book Excerpt from "Generative AI in C++"
  • by David Spuler, Ph.D.

Measuring Code Size and Static Storage

In general, it is more difficult to measure how much space a program is using than to measure how much time it is using. However, most environments provide some means of determining the size of instructions and static data in an executable program. If nothing else, the size of the executable file in overall bytes can be a reasonable guide.

The size command. Under Linux and UNIX, a useful command is the “size” command, which examines an executable program and reports the memory used by its instructions and its global or local static variables. However, it does not (and cannot) report the stack or heap usage because the amount of such memory used is dynamic, and hence cannot be found by analyzing the executable. The command is simply:

    size a.out

This produces output similar to the following:

    text data bss dec hex
    20480 8192 0 28672 7000

The “text” value refers to the machine code instructions for the program code. Both the “data” and “bss” areas refer to global and local static variables. The “data” area refers to variables which have been explicitly initialized with values (e.g. string literals or initialized global variables); the “bss” area refers to variables with implicit initialization which defaults to zero (e.g. global variables or arrays without non-zero initializers).

Function Code Sizes: If the code size is needed on a per-function basis, Linux and most other UNIX environments support the “nm” command. Windows also supports the nm command.

    nm a.out

The nm command differs slightly across older UNIX variants, but will usually print out information including the start and end address of a function, from which the size of a function can be trivially computed.

Link Maps: Window users may be able to use a “link map” report. This allows to find out about executable size by examining the output produced by some C++ compilers at the link stage (although not all compilers will produce useful output). For example, the DOS “link” command with the “/map” option can be used when linking the object files:

    link /map *.obj

 

Next:

Up: Table of Contents

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++