Go to the first, previous, next, last section, table of contents.

Data Type Efficiency Considerations

Novice programmers tend to worry too much about efficiency early on, and too little about it later on. Unless your code is going to run thousands of times, or manipulate thousands of items of some sort, efficiency is not normally likely to be a very important consideration.

Still, one does write programs from time to time which take significant amounts of time or space, and it is worth understanding the approximate space and time costs of the various programming primitives one is using.

Muq muf characters, integers and floats take up no space at all, in the sense that they fit entirely within their variable, property, or stack slot.

Operations on integers and floats take about fifty host clock cycles, as a rough rule of thumb, meaning that integer muf code is likely to be about two orders of magnitude slower than equivalent C code would be. If you need to do serious number-crunching, you probably want to code up a new muf primitive for it to put in inner loop down in C, or perhaps just write a separate server.

Strings of three bytes or less also take "no space": Muq stores them, like ints and chars, entirely within the pointer.

All objects allocated separate memory blocks have about twelve bytes of internal overhead when in memory, with usually another four bytes of ownership information on top of that. Strings also get rounded up to a multiple of four bytes in length, internally, so as to keep everything word-aligned. (All 32-bit machines run faster if operands are 32-bit aligned; many require this.)

If you're going to store thousands of some string, therefor, "yes" is a much better choice than "true": four bytes each vs twenty-four bytes each, approximately, on a 32-bit machine.

Vectors take normal overhead plus four bytes per slot on a 32-bit machine; Symbols are essentially four-slot vectors; Cons cells are essentially two-slot vectors.

Thus, for example, a list of 100 cons cells is going to take about 2K when in ram (sixteen bytes of overhead plus eight bytes of slot, per cell) while a 100-slot vector will take about 416 bytes when in ram.

All operations fetching or storing a value from vectors, symbols, cons cells, stack slots, and local variables can be thought of as taking 50-100 host clock cycles -- about as fast as Muq can do anything. (Any read or write or a property on an object, by contrast, should be thought of as taking thousands of host clock cycles.) Operations on the top two stack locations are fastest; Everything else is operated on by first loading it onto the stack.

Go to the first, previous, next, last section, table of contents.