Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Lines of code and speed?

  1. May 11, 2016 #1
    Here's a question that's been on my mind for a while (I use c#) does the number of lines in your program affect the speed of your program? Lets say you had a program that skipped over one trillion lines, and another who skipped over ten lines. The lines could be inside an if statement who's condition you did not meet. Would any program be faster?

    Thanks in advance
     
  2. jcsd
  3. May 11, 2016 #2

    Paul Colby

    User Avatar
    Gold Member

    If the code is compiled I wouldn't expect it to be effected by sections of code skipped. For a decent interpreter the same reply should hold.
     
  4. May 11, 2016 #3

    jedishrfu

    Staff: Mentor

    If it really had a trillion lines of code then the section following the if block might have to be swapped back into memory from disk and hence would run slower.
     
  5. May 11, 2016 #4

    jim mcnamara

    User Avatar

    Staff: Mentor

    Actually "skipping" over "lines of code" does not carry great meaning. For me, anyway. One line of C# code can call a library which is hundreds or thousands of lines of assembler (machine) code. One line of C# code may represent one line of assembler. There are a more esoteric and extreme examples - like one line of C# code invokes a remote execution call -> run an entire application on another computer in St Louis and you are in Singapore.

    "skipping" means two things - a conditional code branch or a straight jump from memory address A to memory address Q. Conditional branches on modern commodity CPU's can be a source of performance degradation.

    I think you might want to read a slightly dated but very informative article by U Drepper: https://www.akkadia.org/drepper/cpumemory.pdf
    This is really one way answer to your question.

    And if you were gullible enough to execute a trillion line single program image, you should consider any serious consequence like horrible performance your fault -- for running it to start with. o:)
     
  6. May 11, 2016 #5
    My reasoning was that the complier keeps track of line numbers the same way it keeps track of int values. So would the program check each line number until it finds the line number it should be at? For example we did not meet the conditions of an if statement at line 1, to skip over it we must go to line 1 trillion. Would the program check every single line number in between until it finds the number 1 trillion?
     
  7. May 11, 2016 #6
    Yes, but not for the reasons you might think. The size of your functions matter, not the size of the whole program, and here's why. When your computer is set to execute a new command, the first thing it has to do is load it in the CPU's cache. It doesn't just take the one line that's about to run, that'd be wildly inefficient, so what it does is it takes the line you need AND some of the memory blocks afterwards, that way the next command to run is (most likely) the next command in memory, since it's already been pulled into cache, the CPU doesn't need to go fetch it from RAM.

    Also, one LOC is ambiguous. Do you mean a single machine instruction? I can write a single line of C++ that creates hundreds of actual machine instructions.

    To answer your other question: No. Your code will get compiled into something like this assembly:
    Code (Text):
    if x = 0 goto elseCase;
        some code
    elseCase:
        more code
    Your computer knows exactly what line to go to in both the positive and negative case, that "elseCase" label will be translated into a memory address, which is used to store instructions.
     
  8. May 11, 2016 #7
    I think I understand it better now. Thankfully I don't plan on using so many lines of code, but I always try to optimize my programs in as many ways as possible, so knowing this will save me some effort.
     
  9. May 11, 2016 #8

    Mark44

    Staff: Mentor

    This is probably not productive. The compiler can usually a better job of optimization than a typical programmer can.

    The way that optimization is usually done (if you want to try to improve on what the compiler has done), is to use a profiler to see what parts of your program are taking the most time to run, and then optimize that code.
     
  10. May 11, 2016 #9

    fresh_42

    Staff: Mentor

    As a rule of thumb: IOs are far more time expensive than code is, and building index tables for data selections can also be slow, depending on the organisation of the database. In comparison to those code lines are neglectable as long as they don't occupy large amounts of memory at once which doesn't need to be a function of the number of code lines.
     
  11. May 11, 2016 #10

    phinds

    User Avatar
    Gold Member
    2016 Award

    unimaginable that such a beast exists. Even Windows is several lines less that that.
     
  12. May 11, 2016 #11
    Interesting. I'm curious how far does compiler optimization go? I know it simplifies variables names and such.
     
  13. May 11, 2016 #12
    I would have probably chosen a lesser number like 20...but I wanted people to get the overall idea
     
  14. May 11, 2016 #13

    Mark44

    Staff: Mentor

    No, it doesn't simplify variable names. A C++ compiler might "mangle" names, but this entails adding extra characters to function names to distinguish between overloaded functions on a class.

    Optimization is much more complex than what you described. See https://en.wikipedia.org/wiki/Optimizing_compiler.
     
  15. May 11, 2016 #14
    In web programming your code gets minified, which means all the superfluous junk and formatting is erased. This gives performance advantages and lowers the amount of bandwidth required to transfer the web code to the user.
     
  16. May 11, 2016 #15

    jedishrfu

    Staff: Mentor

    Thats because its shipped over as source and interpreted by the browser.
     
  17. May 11, 2016 #16

    phinds

    User Avatar
    Gold Member
    2016 Award

    To add to what jedishrfu said, this has nothing to do with code optimization. Minification does not change anything about code other than the characters that represent the actions. It does make the browser run a bit faster but that's not code optimization. Making the CODE run faster is code optimization.

    If you have really poorly written javascript, after minification it's still exactly the same poorly written javascript, just with fewer characters.
     
  18. May 11, 2016 #17

    phinds

    User Avatar
    Gold Member
    2016 Award

    It depends on the compiler. The first optimizing compiler I became aware of was the FORTRAN "H" compiler back in the 60's but I don't know how extensive it was compared to modern compilers. My recollection is that it was quite good. For example, if you had a loop that worked on a variable that was not subsequently used, it threw out the whole loop (being careful to check that this would not cause unwanted side effects). If you had a statement in a loop that did not need to be in the loop, the compiler would move it to outside the loop.

    Here's a good summary (at a VERY high level) of the differences among optimizing compilers:

    Capture.JPG

    Also, as I recall, optimizing compilers make more passes through the code than regular compilers. I used to know a lot about this stuff but I find that I've forgotten just about all of it.
     
    Last edited: May 11, 2016
  19. May 11, 2016 #18
    I didn't say it had to do with code optimization. All I was suggesting was the lines of code does, in fact, have an impact on performance and speed (in web development), which is what the topic is. The topic isn't necessarily about making the code execute more efficiently based on the title.
     
  20. May 11, 2016 #19
    What did I say that was inconsistent with that? I already explained that.
     
  21. May 11, 2016 #20

    jedishrfu

    Staff: Mentor

    There is no offense taken here. Since the OP asked an open question and since programs are either compiled into machine code or byte and then optimized or they are interpreted from source, its good to cover the bases and discuss these topics.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Lines of code and speed?
  1. Code CVFEM (Replies: 2)

  2. Code tutorials? (Replies: 4)

Loading...