Does using or not block definition in simple "if" influence application performance?

Asked

Viewed 150 times

2

Between one line and another there are some doubts that do not change the flow of decisions at all, but perhaps can influence the performance of the application by the amount of extra lines depending on the way you program.

I like to keep my codes as streamlined as possible, but when it comes to keeping organized I don’t spare lines of code.

In this eagerness to keep a well readable code I always open and close a block of commands as in the example below:

if (a = b) then
begin
  ShowMessage('A é igual a B');
end;

See that there is only one line inside the if, but "I like" always open the begin and end, because tomorrow I have to insert more lines the block is already ready and I don’t have to look for end if, I do it even though I’m nestled in ifs simple, example:

if (a = b) then
begin
  ShowMessage('A é igual a B');
end 
else 
begin
  if (a = c) then
  begin
    ShowMessage('A é igual a C');
  end;
end;

I could reduce the number of these lines ifs, but "like" this way of working.

Now the question of the theme:

The use of more lines as in the examples above, will influence the performance of the application?

  • I imagine that the more independent lines of anything the processor and memory will work a little more, right?
  • Or is the compiler smart enough to ignore the begins when it comes to a if simple?

This doubt came to me because the application already be with thousands and thousands of lines of code, now as quoted, I do not know and these thousands are only lines considered valid by the compiler for the execution of the application or if all lines within the executable.

Another example, but now with "comments"

// Esse bloco de comentário será ignorado pelo compilador?
// Não
// Importa
// Quantas
// Linhas
// Tenha?  
if (a = b) then
  ShowMessage('A é igual a B');

The number of lines above is greater than one if simple with begin and end, but I imagine that the compiler treats different from a command block, because even being comment the processor will have to read the line to know what it is.

An example I particularly detest, but can be more efficient:

if (a = b) then
  ShowMessage('A é igual a B')
else if (a = c) then
  ShowMessage('A é igual a C');

Maybe this may be a useless discussion, but day to day as the application grows we realize that the performance drops a little with each new Form, so any change of habit in the programming that will contribute to improve this performance can be valid.

Note: I gave as an example the Delphi (Pascal), but it is valid for any language.

  • 4

    It would be better if you contextualized it in the form of a question and posted it as your answer. Because as it is, it runs away from how the site works. It is a Q&A, it needs to have a question and an answer. That fits more as an answer.

  • But I would like to know whether or not it influences performance, whether I answer it would not be as if I already knew?

  • 2

    Then turn it into a question, the same one you asked yourself to get this answer you posted. That way, it’s all within the format of the site. ;)

  • 2

    Note: there is no problem in creating a question already posting answer, it is even advisable, if you have some knowledge and want to share, this is welcome!

  • I changed the question

  • 1

    Marcelo, I don’t think you understand. The complete field, including the title and the body, are for questions. If you have a question that you have found the answer and want to share, you should do as per the [tour] explains. You should separate the question from the answer, because as the tour explains, here is a Q&A, this field you used is for the question, the field below, where has the button "Post your answer" is for answer. You should separate what is question and leave in these 2 fields above, and what is solution or answer, post in the field below.

  • Ixi, complicated,I don’t think I really understand, I don’t have the answer, I’m putting what I imagine to tell me if I’m wrong.

  • 1

    @Marcelo is what he’s saying... you asked and posted his vision that nothing else is an answer. Whether you are wrong or not you answered your question in the question field and not the answer.

  • 1

    Would be duplicate of Comments weigh? or just related?

  • 1

    Forget it, leave it as it is, the community decides. If they close, you can question the reason in [meta]

  • @Marcelo I think all the confusion happened because you wrote a redaction to ask something relatively simple. In my view, nothing that is after the yellow frame adds to something in the question and could be removed be content loss. See, for example, the question I quoted above. It asks practically the same thing as you - but with only 2 words.

  • I find it too broad to answer, each interpreter/analyzer can work in a way, has no answer to say whether or not it is costly to all languages, just testing one by one to have some parameter, and sometimes the difference is so small that it is not worth worrying about this for performance, since many languages "compile" for some intermediate language (JIT, of course not all).

  • @Articuno, rsrsrs, sorry ae, really was not understanding, did not know that I should put my vision in the answers.

  • @Marcelo without problems, I had conviction of something, but I ended up getting confused after debating with other users of the site, so I said it was to leave as it is, nor I knew if I was right or not. xD

  • @Anderson Carlos Woss, regarding HTML and javascript as quoted in the link you passed I already knew that by being interpreted by the browser the more lines more cost to the user machine, it reinforces a little my doubt, but as Delphi is compiled I decided to put this question here.

Show 10 more comments

1 answer

3


The use of more lines as in the examples above, will influence the performance of the application?

In Delphi does not influence anything in this case reported. Of course, a poorly written source code can end up generating a target code that wastes resources. But not by comment or by block start and end indicator which is purely syntactic.

I imagine that the more independent lines of anything the processor and memory will work a little more, right?

Not in compiled language, except at the time of compilation, it will still be minimal. In interpreted language has a cost because the compilation occurs at the time of execution and therefore the interpreter has to work harder during execution. But usually the cost is very small and changes almost nothing in the actual execution of the code. See Comments weigh?.

Or is the compiler smart enough to ignore Begins when it comes to a simple IF?

It is not a question of being intelligent, it is his work. The compiler interprets a text written by a human and generates a code that the machine understands. For the machine there is only really useful code, there are syntactic things that only serve to help a human organize his code.

It would be interesting understand what a compiler does. Probably also What is a programming language, IDE and compiler?.

Each encoding style has its own. Very long code tends to be harder to read. Too much squeezed code too. That’s an art. I’ve changed my style a few times, and I think every time I’m in the right style, of course. It takes experience to understand the "best" way. The last code is my favorite. Actually I would do everything in one line, at least in another language.

  • "It’s not a question of being smart, it’s his job" that’s the point, my intention is to facilitate the processor’s work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.