Thread: About code size

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    About code size

    Suppose this two pieces of code:

    Code:
    x = 1;
    b = 2;
    x += b + z;
    if (h) 
    	x--;
    else
    	x++;
    and:

    Code:
    if (h) {
    	x = 1;
    	b = 2;
    	x += b + z;
    	x --;
    } else {
    	x = 1;
    	b = 2;
    	x += b + z;
    	x ++;
    }
    In runtime, both executes the same number of instruction, but example 2 is bigger in size, it has 11 lines of code, first one only have 7.

    So althought runtime instruction are the same, is code 1 faster because the machine need to copy less execute-instruction each time? (I suppose that a so little example would unnoticiable, but a function with hundred of lines would be different)

    (also I suppose a good compiler would optimize the code if correct optimization options are provided).

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The code is bigger because the program behaves differently.

    At run time the source code is gone, simply not there... it's all machine code. Since the if-else construct never executes both if and else, there's no speed hit. The decision has to be made, a jump is executed, the calculations are performed, and the CPU plows off along it's merry way in either example.

    There are situations where you can minimize code size or improve performance but for the most part these are rare and bring only marginal improvements. Basically your code has to do what it has to do to get the job done... speed and size generally take a back seat to stability and maintainability...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Common subexpression elimination - Wikipedia, the free encyclopedia
    One of many techniques used in modern compilers.

    Focus your attention on choosing the best algorithms and data structures for your problem (eg, not using bubble sort for arrays with 1000's of elements), and writing clear code everyone can understand.
    Let the compiler worry about the minutiae.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by CommonTater View Post
    The code is bigger because the program behaves differently.

    At run time the source code is gone, simply not there... it's all machine code. Since the if-else construct never executes both if and else, there's no speed hit. The decision has to be made, a jump is executed, the calculations are performed, and the CPU plows off along it's merry way in either example.

    There are situations where you can minimize code size or improve performance but for the most part these are rare and bring only marginal improvements. Basically your code has to do what it has to do to get the job done... speed and size generally take a back seat to stability and maintainability...
    It is clear to me speed is the same. What I am trying to understand is how runtime flow. It is not a issue about programming, but I want to now about how Operating System run the executable, and as I knows, there is a copy of code in memory which system translate to cpu to run instruction per instruction, so I suppose that forgetting the cpu, the operating system need more time to load code into memory each time a function is to been executed, and as function is bigger in size, the copy phase (maybe a memcpy??) would need more time. Am I wrong?

    thank you for your unswers. (this is all part of a want to understand how runtime works. if you know of any tutorial or www to understand would be wellcame).

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by Salem View Post
    Common subexpression elimination - Wikipedia, the free encyclopedia
    One of many techniques used in modern compilers.

    Focus your attention on choosing the best algorithms and data structures for your problem (eg, not using bubble sort for arrays with 1000's of elements), and writing clear code everyone can understand.
    Let the compiler worry about the minutiae.
    Salem, thank you very much for your advices, which I take into account. In fact during my last years of development I have been improving data and algorithms, and I see now issues that I want to learn (not necessaryly for improve my program, but having my head with its fornitures is better to design and future actions).

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    so I suppose that forgetting the cpu, the operating system need more time to load code into memory each time a function is to been executed
    The operating system (generally speaking) will only load your code once, not each time a particular section gets executed. But again, this is something you should ignore - modern compilers generally optimize for both code size and speed. Just write what's clearest for you to read as a programmer.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Kempelen View Post
    It is clear to me speed is the same. What I am trying to understand is how runtime flow. It is not a issue about programming, but I want to now about how Operating System run the executable, and as I knows, there is a copy of code in memory which system translate to cpu to run instruction per instruction, so I suppose that forgetting the cpu, the operating system need more time to load code into memory each time a function is to been executed, and as function is bigger in size, the copy phase (maybe a memcpy??) would need more time. Am I wrong?
    Yeah, I'm afraid you are.

    You are thinking the source code actually exists inside an executable file... but it's just not there. With C the code you type up on the screen is converted to pure machine code by the compiler then linked into an executable by the linker. At run time the source code is noplace to be found. The executable file, itself is an image of that run-time machine code that is loaded into memory. So the process goes like... create a memory block, load the program image, jump to the entry address... from there on it's all CPU operating directly on machine code instructions in memory.

    Actually the source code only exists as a human convenience in creating the machine language image for the CPU...

    There are some "Interpreted" or "Managed" languages where the process is pretty much as you describe, where a run time translation of the source code is made by the language. C# and .NET are two examples.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by CommonTater View Post
    Yeah, I'm afraid you are.

    You are thinking the source code actually exists inside an executable file... but it's just not there. With C the code you type up on the screen is converted to pure machine code by the compiler then linked into an executable by the linker. At run time the source code is noplace to be found. The executable file, itself is an image of that run-time machine code that is loaded into memory. So the process goes like... create a memory block, load the program image, jump to the entry address... from there on it's all CPU operating directly on machine code instructions in memory.

    Actually the source code only exists as a human convenience in creating the machine language image for the CPU...

    There are some "Interpreted" or "Managed" languages where the process is pretty much as you describe, where a run time translation of the source code is made by the language. C# and .NET are two examples.
    thanks again for the unswer. I knows the difference between compiled and interpreted. Maybe I express myself bad. What I didnt know is how S.O. load the executable into memory. In fact, I dont know why, I thought each time a function is to be executed is copy again into a section of memory. Of course I was wrong, as only once is loaded. What I suppose now is the S.O. make a new data space for the function each time is called (if not, a shared data spece could create conflicts with recursive functions). Again all suppositions......

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Personally, I'd take the original code and make it into
    Code:
      x = z + (h ? 2 : 4);
      b = 2;
    but maybe I'm missing the point of the original question. No mucking around with incrementing and decrementing x. One comparison (of h with zero), an addition, and two assignments.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes, new data is created each time a function is called. This is actually done on the program's "stack" where the CPU pushs and pops registers for machine language manipulations. Only one copy of the function exists, but each time the function is called it's parameters and any variables created inside are pushed onto the stack then popped back off when the function exits. This is actually done at the curly braces in your source code, creating "scopes" in your program.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Kempelen View Post
    What I suppose now is the S.O. make a new data space for the function each time is called (if not, a shared data spece could create conflicts with recursive functions).
    Like CommonTater says, that's exactly the situation the stack is used to prevent - if you really want to see how these things work "under the hood" then some assembler programming experience is invaluable. I can recommend "programming from the ground up" is freely available (just google) if you program on Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code size
    By KIBO in forum C Programming
    Replies: 8
    Last Post: 04-12-2007, 10:15 AM
  2. Char size (in bytes) independent code
    By JoshR in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2005, 07:33 PM
  3. Code and data size
    By Roaring_Tiger in forum C Programming
    Replies: 1
    Last Post: 03-24-2003, 10:16 PM
  4. Font size n ASCII code
    By kewell in forum C Programming
    Replies: 4
    Last Post: 07-15-2002, 09:25 AM
  5. font size and ASCII code
    By kewell in forum Windows Programming
    Replies: 1
    Last Post: 07-15-2002, 02:35 AM