Do More Lines of Code Affect Program Speed?

In summary: RAM.If you have a billion lines of code, it might take several seconds to run. If you have a trillion lines of code, it might take several minutes to run.
  • #1
kolleamm
477
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
  • #2
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.
 
  • #3
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.
 
  • #4
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 fresh_42 and jedishrfu
  • #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?
 
  • #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:
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 kolleamm
  • #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.
 
  • #8
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 harborsparrow and kolleamm
  • #9
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 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 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 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 kolleamm

What are lines of code and how do they affect speed?

Lines of code refer to the number of instructions or statements in a software program. The more lines of code a program has, the longer it may take to execute. However, the complexity and efficiency of the code also play a significant role in determining the speed of a program.

Is there a specific number of lines of code that guarantees faster execution?

No, there is no specific number of lines of code that guarantees faster execution. The speed of a program depends on various factors such as the programming language, hardware specifications, and coding practices.

How can I reduce the number of lines of code in a program?

One way to reduce the number of lines of code in a program is by using coding techniques like abstraction, encapsulation, and inheritance. These techniques help in writing concise and efficient code, which can lead to faster execution.

Does removing unnecessary lines of code always improve speed?

Not necessarily. While removing unnecessary lines of code can improve speed, it is essential to consider the impact it may have on the overall functionality of the program. Removing lines of code that are critical for the program's functionality can result in errors and slower execution.

Are there tools that can help optimize code for speed?

Yes, there are various tools and techniques available that can help optimize code for speed. These include compilers, debuggers, and profilers that can identify and fix potential performance issues in a program.

Similar threads

  • Programming and Computer Science
Replies
22
Views
747
  • Programming and Computer Science
Replies
21
Views
1K
  • Programming and Computer Science
Replies
1
Views
726
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
2
Replies
66
Views
4K
Replies
10
Views
955
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top