Using the C++ Math Library | ![]() ![]() |
The Space-Time Continuum
Faster, smaller, cheaper: choose any two. It is well-known that programs can be made more space efficient at the cost of decreasing their time efficiency, and vice versa. The code in the C++ library makes trade-offs, as described below, in an attempt to execute as rapidly as possible, without using excessive amounts of memory.
Time
The MATLAB C++ Math Library is implemented on top of the MATLAB C Math Library, which in turn is a layer above the raw MATLAB code. Despite this layering, the C++ library code performs well. The intermediate layers consume less than 1% of a typical program's CPU time.
During the development of the C++ library, one of the greatest increases in speed resulted from the implementation of a block-caching memory manager. This is a classic example of trading space for time. The space cost of maintaining an internal list of memory blocks eliminates the time cost of a system call to malloc()
. This time savings can be quite significant. On the PC, for example, this system resulted in a seven-fold increase in speed.
Space
There are two major motivations for a space-efficient implementation. The first motivation is the obvious one: the more arrays that you create or the larger arrays that you create, the more interesting problems you can solve. The second motivation is less obvious but equally important: allocating blocks of memory is slow and, thus, the fewer allocated, the better the program's performance.
C++ is notorious for copying objects and automatically creating and destroying many temporary objects. This behavior is particularly common in arithmetic expressions and in passing arguments to functions. Since these temporary objects cannot be avoided, it is very important that they be inexpensive, both in terms of time and space.
The current implementation uses a reference-counting scheme to minimize the size of a copy. Using this scheme, each copy requires at most an additional eight bytes, regardless of the size of the copied array. Aside from allocating the space, no additional computation is necessary to make the copy. This representation is quite efficient. Since MATLAB functions and operations have no side effects, and the assignment operator practices copy-on-write, reference counting is safe.
![]() | Performance and Efficiency | Array Input and Output | ![]() |