Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:42:36+00:00 2026-06-10T21:42:36+00:00

Consider, for example: int sum(int a, int b) { return a + b; }

  • 0

Consider, for example:

int sum(int a, int b)
{
    return a + b;
}

vs.

int sum(const int a, const int b)
{
    return a + b;
}

Is the second approach in general faster?

Function parameters in C are copied and sent to the function, so that changes inside the function do not affect the original values. My reasoning is that in the second sum above, the compiler knows for sure that a and b are not modified inside the function, so it can just pass the original values without copying them first. That’s why I think the second sum is faster than the first. But I don’t really know. In the particular simple example of sum above, the differences, if any, should be minimal.

Edit: The sum example is just to illustrate my point. I don’t expect that in this particular example there should be large differences. But I wonder if in more complicated situations the const modifier inside a function parameter can be exploited by the compiler to make the function faster. I doubt that the compiler can always determine whether a parameter is changed inside a function (hence my 2nd question below); hence I’d expect that when it finds a const modifier, it does something different than when there’s no const modifier.

Question: In general, a function will be faster when its arguments are const, than when they are not?

Question 2: In general, can a C compiler (theoretically) always determine whether a function parameter is changed inside the function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:42:38+00:00Added an answer on June 10, 2026 at 9:42 pm

    Short answer: No

    Long answer, no, with proof.

    I ran this test, a couple of times, and saw no real time difference, on my MacBook pro compiled with clang:

    int add(int a, int b)
    {
        return a + b;
    }
    
    const int cadd(const int a, const int b)
    {
        return a + b;
    }
    
    int main (int argc, char * argv[])
    {
    #define ITERS 1000000000
    
        clock_t start = clock();
        int j = 0;
        for (int i = 0; i < ITERS; i++)
        {
            j += add(i, i + 1);
        }
    
        printf("add took %li ticks\n", clock() - start);
    
        start = clock();
        j = 0;
        for (int i = 0; i < ITERS; i++)
        {
            j += cadd(i, i + 1);
        }
    
        printf("cadd took %li ticks\n", clock() - start);
    
        return 0;
    }
    

    Output

    add took 4875711 ticks
    cadd took 4885519 ticks
    

    These times really should be taken with a grain of salt, however, as clock isn’t the most accurate of timing functions, and can be influenced by other running programs.

    So, here is the compared assembly generated:

    _add:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register %rbp
        movl    %edi, -4(%rbp)
        movl    %esi, -8(%rbp)
        movl    -4(%rbp), %esi
        addl    -8(%rbp), %esi
        movl    %esi, %eax
        popq    %rbp
        ret
    
    _cadd:                                 
        .cfi_startproc    
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register %rbp
        movl    %edi, -4(%rbp)
        movl    %esi, -8(%rbp)
        movl    -4(%rbp), %esi
        addl    -8(%rbp), %esi
        movl    %esi, %eax
        popq    %rb
    

    So, as you can see, there is No difference between the two. Passing an argument as const is only a hint to the caller the the argument will not be changed, and in a simple scenario like the one described above, will not result in any different assembly compiled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider this example, how do we force implicit conversion in function whose second
Consider the following example: int size = 10, *kk = new int[size]; for (int
Consider the following example program: next :: Int -> Int next i | 0
Consider the below example int nCount[2] = {5,10}; int* ptrInt; ptrInt = nCount; cout<<ptrInt<<Endl;//this
Consider this example: #include <iostream> #include <string> #include <vector> #include <iterator> int main() {
Consider following example: #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <hiredis/hiredis.h> int main(int argc,
Consider the following C++ code example: int array_1[] = {5,6,7}; int array_2[] = {6,7,8,9};
Consider the following example: struct MyStruct { int a; int b; }; I can
Consider this example: <% int testNumber = 1; %> //Some HTML goes here <%=testNumber%>
Consider this example: struct Nobody_Expects_The_Spanish_Inquisition{}; int main(){ throw Nobody_Expects_The_Spanish_Inquisition(); } Output shown on Ideone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.