const ??

I tried looking at a few websites but couldn't really get a good ans.

Why is :
Fn( std::vector<Class AA>& ) faster than Fn(const std::vector<class AA> &)

What kind of optimization takes place when I use the const keyword ? Given it sends the same address/reference ?

Sincerely,
navderm
Interesting. I had no idea there was a difference in performance between the two. As far as I knew, the const keyword merely instructs the compiler to make sure no non-const methods are accessed through the parameter/variable, and that no variables are written via those methods that ARE const. As far as I knew, this is merely a compilation check and the end result was the same.

How did you determine there's an improvement in performance?
closed account (zb0S216C)
Any piece of storage that's declared constant is flagged as ROM (read-only memory) when the program is compiled. The OS can see this information and knows not to allow any modifications to this storage.

Since a constant piece of storage cannot change, there's no need for the CPU to check for changes in its value, and therefore, does not need to waste time fetching its new value from memory. As a result, the CPU can keep this value in its cache if it wants to -- chances are it will -- and as you know, the CPU's cache is much, much faster than RAM.

@webJose: Maybe (s)he used a profiler?

Wazzak
Last edited on
Dunno, Framework.

And what you say is contrary to what I read somewhere about constness. Maybe I got it wrong. I knew about what you are saying but that applies for const field values only and not the whole const correctness enforcement. After all, only fields can hold values, and your explanation is about values.

Anyway, regardless of that I guess your explanation is sound, but the OP actually states it the other way arround: He/She states that the non-const version is faster. Intriguing, isn't it?
closed account (zb0S216C)
Yes, it's strange indeed.

@navderm: How are you passing the actual parameters?

Wazzak
@Framework :
I am passing pretty big arrays of values by reference
std::vector& / std::list& .

But I understand your point about CPU not doing anything when const is passed to do any checking.

More so, I'll try to find more about what is going on with my piece of code, may be the difference isn't even big enough to get concerned as compared to the whole project working time.
~Neeraj
Topic archived. No new replies allowed.