Lines of code and speed?

  • Thread starter kolleamm
  • Start date
  • #1
374
34

Main Question or Discussion Point

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
 

Answers and Replies

  • #2
Paul Colby
Gold Member
1,028
228
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
11,485
5,014
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
jim mcnamara
Mentor
3,784
2,110
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:)
 
  • #5
374
34
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
1,516
617
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.
 
  • #7
374
34
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
33,265
4,965
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.
 
  • #9
12,648
9,167
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.
 
  • #10
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
15,907
5,579
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
374
34
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.
 
  • #12
374
34
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
33,265
4,965
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
117
6
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
11,485
5,014
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
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
15,907
5,579
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
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
15,907
5,579
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:
  • #18
117
6
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
117
6
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
11,485
5,014
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
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
15,907
5,579
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
11,485
5,014
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.
 

Related Threads on Lines of code and speed?

  • Last Post
Replies
3
Views
681
Replies
7
Views
3K
Replies
3
Views
541
Replies
2
Views
2K
  • Last Post
Replies
1
Views
8K
Replies
2
Views
4K
  • Last Post
Replies
8
Views
1K
Replies
1
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
8
Views
29K
Top