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

  • Home
  • SEARCH
  • 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 6223111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:25:51+00:00 2026-05-24T08:25:51+00:00

I was running the code below, which essentially does very little. It just adds

  • 0

I was running the code below, which essentially does very little. It just adds 2 and four 100 million times and outputs the runtime.

#include "time.h"
#include <iostream>
using namespace std;

void add (){
int tot = 2+4;
}

void main(){
int t = clock();
int count = 0;
while(count<100000000){
    int tot = 2+4;
    count++;
}
cout <<endl<< "runtime = " << fixed <<(double) (clock() - t) / CLOCKS_PER_SEC <<"s" << endl;

}

But I was interested to see the time difference when doing the exact same thing but calling a function. So I replaced the line “int tot = 2+4” with “add()”.

I was expecting the second runtime to be slightly longer but was a lot longer. First implementation = .3s and second implementation = 3s.

I understand calling the function requires using the stack to store the return address and store local data. But it must be doing a lot more then this?

Would be great if someone could explain to me what exactly causes the big difference in runtime or maybe I am doing something silly.

  • 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-05-24T08:25:52+00:00Added an answer on May 24, 2026 at 8:25 am

    As mentioned already by Seth, inline functions would probably get optimized.

    In the 1st case (with basic optimizations on) most likely instead of constantly adding those 2 numbers it will resolve 2 + 4 to 6 and simply just do a simple

    mov eax, 6 ;eax is just an example here or
    mov tot_addr, 6 ; mem->mem mov
    

    In the 2nd case since it is a function call the system has to

    push 4 ;assuming 4 byte ints
    push 4 ;wrote 2 to be clear that you have to push both variables
    call add
    

    or something along these lines. As the call stack needs to be created for that function (ommitted return value push and such for simplicity). Once this function returns the stack pointer needs to be moved back previous to that first push and then the RET will set the instruction pointer back. As you can see this is more expensive than doing a simple

    mov eax, 4
    add eax, 2
    

    which is likely to be the case if you were doing just a simple (non optimized add)

    EDIT: Here’s some more information about in-lining the function. When you inline the function it simply takes whatever functionality the function itself would do and places the instructions directly where it is reference instead of doing a CALL instruction and setting up a function call directly. For instance instead of

    mov eax, 4
    mov ecx, 2
    push 4 ; size for return value
    push eax
    push ecx
    call add
    

    You will end up with

    mov eax, 4
    mov exc, 2
    add eax, ecx
    

    In code terms:

    int a = 4;
    int b = 2;
    int res = add(a, b);
    

    would become

    int a = 4;
    int b = 2;
    int res = a + b;
    

    assuming you inline the add function.

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

Sidebar

Related Questions

Hello friends i am running code given below which contains the setLogTimeEntery function and
I have tritten below code in which I am running two jobs. First with
When running the below code a type is never returned, despite there being a
I get this message when running the code below: There is an error in
I have the below code which is suppose to show a list of contact
I have the code below running in my application and it takes approx 2
I have some code running on my website which will detect if a div
Q1. What is a condVar in Java? If I see the code below, does
I've a small problem with the code below. It is a simple program which
I've got the following code below which parses a text file and indexes the

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.