#define INT_ALIGN_MASK (sizeof(int) - 1) #include /* * MemSet * Exactly the same as standard library function memset(), but considerably * faster for zeroing small word-aligned structures (such as parsetree nodes). * This has to be a macro because the main point is to avoid function-call * overhead. However, we have also found that the loop is faster than * native libc memset() on some platforms, even those with assembler * memset() functions. More research needs to be done, perhaps with * platform-specific MEMSET_LOOP_LIMIT values or tests in configure. * * bjm 2002-10-08 */ #define MemSet(start, val, len) \ do \ { \ int * _start = (int *) (start); \ int _val = (val); \ size_t _len = (len); \ \ if ((((long) _start) & INT_ALIGN_MASK) == 0 && \ (_len & INT_ALIGN_MASK) == 0 && \ _val == 0 && \ _len <= MEMSET_LOOP_LIMIT) \ { \ int * _stop = (int *) ((char *) _start + _len); \ while (_start < _stop) \ *_start++ = 0; \ } \ else \ memset((char *) _start, _val, _len); \ } while (0) #define MEMSET_LOOP_LIMIT 1024 int main(int argc, char **argv) { int i,j; char *buf; buf = malloc(256); j = 32; for (i = 0; i < 1000000; i++) MemSet(buf, 0, j /* choose 256 or j here */); return 0; }