Stories
Slash Boxes
Comments

SoylentNews is people

posted by Fnord666 on Sunday August 25 2019, @01:57PM   Printer-friendly
from the quite-the-contrary dept.

Submitted via IRC for SoyCow3196

Why const Doesn't Make C Code Faster

In a post a few months back I said it's a popular myth that const is helpful for enabling compiler optimisations in C and C++. I figured I should explain that one, especially because I used to believe it was obviously true, myself. I'll start off with some theory and artificial examples, then I'll do some experiments and benchmarks on a real codebase: Sqlite.

Let's start with what I used to think was the simplest and most obvious example of how const can make C code faster. First, let's say we have these two function declarations:

void func(int *x);
void constFunc(const int *x);

And suppose we have these two versions of some code:

void byArg(int *x)
{
  printf("%d\n", *x);
  func(x);
  printf("%d\n", *x);
}

void constByArg(const int *x)
{
  printf("%d\n", *x);
  constFunc(x);
  printf("%d\n", *x);
}

To do the printf(), the CPU has to fetch the value of *x from RAM through the pointer. Obviously, constByArg() can be made slightly faster because the compiler knows that *x is constant, so there's no need to load its value a second time after constFunc() does its thing. It's just printing the same thing. Right? Let's see the assembly code generated by GCC with optimisations cranked up:


Original Submission

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 2, Insightful) by Anonymous Coward on Sunday August 25 2019, @05:26PM (3 children)

    by Anonymous Coward on Sunday August 25 2019, @05:26PM (#885249)

    JavaScript runs on the consumer end. Nobody gives a shit about how fast it because we don't fucking run it. As long as we get that sweet sweet data to the advertising server so we can sell targeted ads to the fucking idiot consumer. Nothing else matters.

    Starting Score:    0  points
    Moderation   +2  
       Insightful=1, Informative=1, Total=2
    Extra 'Insightful' Modifier   0  

    Total Score:   2  
  • (Score: 2) by maxwell demon on Sunday August 25 2019, @08:38PM (2 children)

    by maxwell demon (1608) on Sunday August 25 2019, @08:38PM (#885362) Journal

    And in which way are the properties of JavaScript relevant for the optimization of code written in C or C++?

    --
    The Tao of math: The numbers you can count are not the real numbers.
    • (Score: 1, Funny) by Anonymous Coward on Sunday August 25 2019, @09:25PM (1 child)

      by Anonymous Coward on Sunday August 25 2019, @09:25PM (#885384)

      You're not getting the message, snowflake. C is irrelevant because C is not used in the real world to sell targeted advertising. JavaScript makes money.

      • (Score: 0) by Anonymous Coward on Monday August 26 2019, @11:00AM

        by Anonymous Coward on Monday August 26 2019, @11:00AM (#885592)

        Still mining Monero on other people's computers? Tse, tse.