stupid question: how to use malloc/new efficiently
Tim Jansen
tim at tjansen.de
Sat Jan 17 19:25:03 CET 2004
On Saturday 17 January 2004 16:26, Alexander Neundorf wrote:
> I never thought about it until I recently read it somewhere:
> should one always use malloc/new for buffers with sizes which are multiples
> of 1K/1 page i.e. usually 4K or something like this ?
This should be really the last optimization resort, but it can have advantages
if your buffer size is a power of 2. You need to make sure that not only the
page size is correct, but the begin of the buffer is also aligned (thus for
4K pages the address needs to be a multiple of 4096). Then, if the memory is
swapped out, the kernel may need to swap only one page in instead of two.
A similar optimization is possible with the CPU caches. CPUs usually work with
'cache lines'. If your data structure fits into one or more cache lines, only
these cache lines need to be fetched. Like with pages you need to align the
data correctly.
Note that both optimizations do not make sense for sequential data access,
only for random access. On the Intel developer site you can find lots of
documents that explain how to align your data and optimize for optimal cache
use.
bye...
More information about the Kde-optimize
mailing list