S
S
Soul12021-03-27 23:27:17
IT education
Soul1, 2021-03-27 23:27:17

Does the number of lines of code affect the speed of program execution?

The question is about Python, it is primarily of interest, but it is interesting to hear about other programming languages ​​as well. It’s just that for some reason they don’t write about this at all in textbooks, although the question is obvious and suggests itself. It is generally important for a computer how many lines we will scatter our code? Or does he care?
I understand that most often the readability of the code will always come first, but let's say we are considering an example when you write code for yourself and you can write the entire code at least in one line, or you can scatter it over 100 lines.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
G
GavriKos, 2021-03-27
@GavriKos

The number of rows does not affect - affects the number of operations.

S
Saboteur, 2021-03-27
@saboteur_kiev

The program has critical and not so critical moments.
Critical for speed optimize as much as possible, non-critical - no

V
Vasily Bannikov, 2021-03-27
@vabka

It is generally important for a computer how many lines we will scatter our code?

The computer executes machine code, not source code. So the number of lines of code in the source, as a rule, does not affect the speed of execution.
In the case of python, in theory, performance may change slightly from changing the number of lines, as it interprets the code (although there is also JIT, which will not care), but the speed can change in both directions.
My opinion about performance:
1. First write clearly
2. Then use more efficient algorithms
3. Naturally, we must also take into account that in some cases it is possible to parallelize calculations and / or use SSE, AVX, C-extensions.
4. When exactly all the first three stages have been passed, start optimizing by shortening variable names, removing extra lines, and using any other exotic optimization methods (you also need to check everything with benchmarks). Some of these manipulations can certainly be automated.

M
mkone112, 2021-03-29
@mkone112

Certainly! If you have a source file with one file per 10GB, it will run slowly, slowly. Well, not every compiler will cope with the Hindu code-sheet - it can turn out to be much slower.

A
ASurt, 2021-04-16
@ASurt

As already mentioned, it is not the number of rows that affects, but the operations.
I add:
Strings can be different

l = [[0 for _ in range(100)] for _ in range(100)]
x = 1 + 1

As the simplest example. The first line will obviously take longer than the second. Python allows you to make very time-consuming one-liners that could even be made into functions.
Plus, the use of library methods that look like one line of code, but in reality can have very complex logic behind them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question