After this topic it became interesting.
Given a set of values, subject to a certain progression:

A = {x: x(i) < x(i+1) и x(i+1)=2*x(i)} 

We must compare them with the input data.
What will be faster: a loop or N times to write ifelse?

Such things as the readability of the code can be neglected.
I did not find similar things in the internet, because usually compare the performance of similar operations.

Ps the question is more theoretical, so that any yap.

  • If this is an algebraic or geometric progression, then for large N it is probably more productive to calculate the progression term i for the number from the incoming stream, but this is a subjective opinion. - ReinRaus
  • I thought about it. But in essence, the interest was different. - zenith


6 answers 6

Option 1 - The cycle or the condition of the difference will not be, because it will still be about the same number of comparisons and jumps. Since we do not calculate but comparing.

Option 2 - Conditions will work slower since it will take more code, and we will get a penalty due to the additional load from memory and cache missions. (The effect, of course, will be on large N).

In general, it’s not just the language and the compiler, but also the processor. Measure and try, but in any case these are micro-optimizations that you shouldn't go down to in production.

  • In igrodel happens on micro-optimization huge resources are won. - Gordio

If we take into account the time of writing ifs, then the cycles are really faster.

  • This is five plus. =) - knes

In fact, modern compilers can optimize code. True, they do it not in the best way, but often this is enough. One of the standard methods is to unroll cycles into a linear code, because it is more convenient for the processor to execute a code without branching. Even during optimization, it is necessary to take into account the peculiarities of the processor architecture, in particular, how they work with RAM. And it turns out that by optimizing the code for one of the processors, you can get a completely non-optimal code for others. Well, and besides, there are different optimization criteria. There are two main ones: by the speed of code execution and by the code size. So, the compilers introduce keys for the exact task for what and how you want to optimize.

So, in your case, you should try to test different versions of the program and conclude that it is better, independently. Moreover, it is desirable to check with different compilation modes.

  • one
    "True, they do it in a bad way" is not entirely true. Most often, if the compiler does something "wrong" - the programmer is to blame for this - Zowie

@zenith - in this case, you are not optimizing the code better than the compiler.

If the code can be reduced to linear - it will be given to it. Accordingly, to engage in such reflections is only if you write to ASM

And in languages ​​where there is not even a JIT compiler, this, at least, will not increase performance, as a maximum - it will make it even worse ... (and do not forget about 100500 if, else blocks)

    It saves on matches. In web development, it doesn’t matter what is faster: if or for, since waiting time for a response from the database is incomparably greater in performance gains from replacing for with if or vice versa.

    On the other hand, I recommend reading about the Chinese code in the article http://lurkmore.to/Indussky_code

      Such things as the readability of the code can be neglected.

      Are you serious now ?! Neglecting the readability of the code is like walking on the street in torn dirty clothes, do you wear such clothes? I think not. Then why do you not care about the purity of the code?

      What will be faster: a loop or N times to write ifelse?

      And if you need to check 100 times? Will you write if else 100 times?

      If you are so interested to know what is faster, then test for measuring the execution time of the code.

      PS use the cycle and do not clog the head with the necessary nonsense.

      • I would not mention that the question is theoretically if it were not. Of course, from a reasonable point of view, one cycle is perceived better than 1lyfs. But the question is not that? And your controversy turns out not to the topic. - zenith
      • read again my answer .... If you are so interested to find out what is faster, then test for measuring the execution time of the code. PS use the cycle and do not clog the head with the necessary nonsense. - KoCaTKo
      • @Jetu - a person asked an absolutely normal question. The fact that you are not interested in such low-level things does not mean that the question is nonsense. "not necessary nonsense"? By your logic, ASM is also unnecessary nonsense, but if it were not there, it would be machine code at all :) - Zowie
      • Look at what questions the person asked, tell me how the web programmer is connected with the assembler? and where did you see the asm? the person asked what is faster with a cycle or conditions? I answered him, conduct a test at the time of execution, I will not conduct these tests for him. It seems that no one reads the answer to the end ...... - KoCaTKo
      • one
        "and where did you see the asm?" yes he is everywhere. Even when you write JS, in the Orthodox browsers, the most "hot" features, etc. compiled into ASM (ideally linear). You did not give a direct answer. By the same, rather, you did not read the question. I quote: "Ps the question is more theoretical . " You wrote it as if you thought that the CU would really apply such an approach. - Zowie