Passing array as const slows down code?
Joseph Rushton Wakeling
joseph.wakeling at webdrake.net
Fri Apr 27 08:12:07 PDT 2012
Hello all,
A surprise today when tweaking some code.
I have a function which takes as input an array of values (the values are a
custom struct). So, in pseudocode, it's something like this:
struct MyStruct
{
size_t x;
size_t y;
double z;
}
void foo(MyStruct[] input)
{
...
}
Since the input isn't modified, I thought I'd tag it as const[1]:
void foo(const MyStruct[] input) { ... }
... but this turned out to noticeably slow down the program. (Noticeably as in,
a 25-second as opposed to 23-second running time, consistently observed on
repeated trials of different code versions.)
I'm surprised by this as I would have thought if anything tagging a variable as
const would if anything have increased the speed, or at worst left it the same
as before.
Can anyone explain why? It's no big deal, but I'm curious.
Thanks and best wishes,
-- Joe
-------
[1] Actually I originally tried marking it as immutable, but it turns out that's
a stronger condition that doesn't just apply to what happens _inside_ the function.
More information about the Digitalmars-d-learn
mailing list