Live data from Hacker News

Why Const Doesn't Make C Code Faster

theartofmachinery.com

1–10 of 197 posts

Re: Why Const Doesn't Make C Code Faster

#4
post #2

overlooks the value of const qualification for the caller - if the argument is marked as const, the caller might reasonably assume the data won’t be changed.

Yes, there are many cases where the caller can optimize the dependency chain / order of

    constFoo(x);
    constBar(x);
where it couldn't make that assumption if one function wasn't const.

Re: Why Const Doesn't Make C Code Faster

#5
I have done a lot of profiling work and I have observed similar things. One thing is const, another is virtual functions. A lot of people think they add overhead and avoid them for performance reasons but my profiling almost never showed them as a problem. Same for const and inline. it’s really hard to predict what the optimizer will do.

Obviously there are stupid things that can be avoided from the start but in general I prefer clean code where people write for readability and simplicity and not speed.

Re: Why Const Doesn't Make C Code Faster

#6

I have done a lot of profiling work and I have observed similar things. One thing is const, another is virtual functions. A lot of people think they add overhead and avoid them for performance reasons but my profiling almost never showed them as a problem. Same for const and inline. it’s really hard to predict what the optimizer will do. Obviously there are stupid things that can be avoided from the start but in gene…

Yes, profile. Anecdotally, I once removed a number of virtual function calls as they were imposing significant overhead in a tight loop (image processing). Would that be the case today? No idea; I'd have to measure it again.

Re: Why Const Doesn't Make C Code Faster

#8
I've worked with JavaScript weenies who are in love with the const declaration. They don't have an answer, though, when I ask, "Shouldn't the compiler be able to notice that a variable isn't changed in it's lifetime?" Most of the const values are just local and never leave the function but the weenies continue to stomp their feet when I use "var".

The compilers are smart. Let them do their thing.

Re: Why Const Doesn't Make C Code Faster

#9
post #2

overlooks the value of const qualification for the caller - if the argument is marked as const, the caller might reasonably assume the data won’t be changed.

I don't think he's saying that you shouldn't use const (if he is, I vehemently disagree!) just that using it won't make the code faster. It will definitely prevent you, or the next maintenance programmer, from making a lot of dumb mistakes.

Re: Why Const Doesn't Make C Code Faster

#10
post #8

I've worked with JavaScript weenies who are in love with the const declaration. They don't have an answer, though, when I ask, "Shouldn't the compiler be able to notice that a variable isn't changed in it's lifetime?" Most of the const values are just local and never leave the function but the weenies continue to stomp their feet when I use "var". The compilers are smart. Let them do their thing.

It tells the reader something too. There's no excuse for var anymore.
Post reply on HN