Do More Lines of Code Affect Program Speed?

  • Thread starter Thread starter kolleamm
  • Start date Start date
  • Tags Tags
    Code Lines Speed
Click For Summary

Discussion Overview

The discussion centers around the question of whether the number of lines of code in a program affects its execution speed, particularly in the context of programming in C#. Participants explore various aspects of code execution, compilation, and optimization, raising both theoretical and practical considerations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant questions if a program with a trillion lines of code would run slower than one with ten lines, particularly if the lines are skipped due to conditional statements.
  • Another participant suggests that if the code is compiled, the execution speed would not be affected by skipped sections, and a decent interpreter would behave similarly.
  • Concerns are raised about the practical implications of executing a program with a trillion lines, including potential memory swapping issues that could slow down execution.
  • It is noted that a single line of C# code can represent complex operations, including calls to extensive libraries or remote executions, complicating the notion of "skipping" lines of code.
  • Some participants discuss how modern CPUs handle command execution and caching, indicating that the size of functions rather than the total line count may be more relevant to performance.
  • There is a debate about the effectiveness of manual optimization versus compiler optimization, with suggestions that compilers often perform better optimizations than individual programmers.
  • Participants discuss the concept of minification in web programming, clarifying that while it reduces file size for transfer, it does not equate to code optimization that improves execution speed.
  • Questions arise about the extent of compiler optimizations, with references to historical compilers and their capabilities compared to modern ones.

Areas of Agreement / Disagreement

Participants express a range of views on the relationship between lines of code and program speed, with no consensus reached. Some argue that the number of lines is less significant than how code is structured and executed, while others emphasize the potential impact of large codebases on performance.

Contextual Notes

Discussions include assumptions about how compilers and interpreters handle code execution, the implications of conditional branches on performance, and the nuances of code optimization versus minification. Specific mathematical or technical claims remain unresolved.

Who May Find This Useful

Programmers interested in code optimization, software performance, and the intricacies of compiler behavior may find this discussion relevant.

kolleamm
Messages
476
Reaction score
44
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? Let's 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
 
Technology news on Phys.org
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.
 
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.
 
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:)
 
  • Like
Likes   Reactions: fresh_42 and jedishrfu
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?
 
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:
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.
 
  • Like
Likes   Reactions: kolleamm
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.
 
kolleamm said:
but I always try to optimize my programs in as many ways as possible
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.
 
  • Like
Likes   Reactions: harborsparrow and kolleamm
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.
 
  • Like
Likes   Reactions: kolleamm
  • #10
jim mcnamara said:
And if you were gullible enough to execute a trillion line single program ...
unimaginable that such a beast exists. Even Windows is several lines less that that.
 
  • #11
Mark44 said:
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.
Interesting. I'm curious how far does compiler optimization go? I know it simplifies variables names and such.
 
  • Like
Likes   Reactions: harborsparrow
  • #12
phinds said:
unimaginable that such a beast exists. Even Windows is several lines less that that.
I would have probably chosen a lesser number like 20...but I wanted people to get the overall idea
 
  • #13
kolleamm said:
Interesting. I'm curious how far does compiler optimization go? I know it simplifies variables names and such.
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.
 
  • #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.
 
  • #15
serp777 said:
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.

Thats because its shipped over as source and interpreted by the browser.
 
  • #16
serp777 said:
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.
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.
 
  • #17
kolleamm said:
Interesting. I'm curious how far does compiler optimization go?
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:
  • Like
Likes   Reactions: kolleamm and jedishrfu
  • #18
phinds said:
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.

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.
 
  • #19
jedishrfu said:
Thats because its shipped over as source and interpreted by the browser.
What did I say that was inconsistent with that? I already explained that.
 
  • #20
serp777 said:
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.

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.
 
  • #21
serp777 said:
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.
I get your point, but you can have lots of lines of well written code run faster than the same algorithm written in fewer lines but written badly, so as a generalization I don't agree w/ you (assuming you mean that fewer lines means faster execution, which I believe IS what you mean).
 
  • #22
As I think we have thoroughly discussed this topic, I'm closing the thread before I have to hand out warnings or moderate it further.

Thanks everyone for your time and comments, hopefully the OP understands the nature of his question now:

trillions of lines are bad and fewer lines are good.
 
  • Like
Likes   Reactions: kolleamm

Similar threads

  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
7K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
10
Views
2K
Replies
2
Views
3K
  • · Replies 49 ·
2
Replies
49
Views
4K