+ const unsigned long timing[] = { 5, 100, 500};
You'll save space and time if you also make this array "static",
otherwise the compiler will build the array every time this function is
called.
No, *const* specifier is enough to not build this array every time. It will be put into the .const section.
gcc disagrees:
$ cat const_static.c...
int f(int x) {
const unsigned long timing[] = { 5, 100, 500};
static const unsigned long timing2[] = { 5, 100, 500};
return timing[x] + timing2[x];
}
$ gcc -Os -S const_static.c
$ cat const_static.s
timing2.0:
.long 5
.long 100
.long 500
...
movl 8(%ebp), %edx
movl $5, -16(%ebp)
movl $100, -12(%ebp)
movl $500, -8(%ebp)
movl timing2.0(,%edx,4), %eax
addl -16(%ebp,%edx,4), %eax
Regards,
Clemens