How many times slower is the processor performing the transition to an arbitrary instruction compared to the usual transition to the next instruction?


As far as I know, when writing programs (both in high and low level languages), you should try to avoid both conditional and unconditional transitions (goto and if .. then ..). So, how many times faster two sequential instructions will be executed

mov AX,$5435; mov LX, AX; 

in comparison with

 m1: ... cmp bx, n; jne m1; 

(Do not judge for errors, if any, with the assembler is almost not familiar).


The fact is that I am writing a program on delphi, consisting of 2 cycles with more than 1000 interactions each. In fact, these two cycles could be merged into one, but the code is very cumbersome, and I wrote it a long time ago (I’ll have to figure it out), and then the interactions would be 2 times less. Therefore, I asked this question to make sure that my code is rewritten.

  • In principle, the question is interesting and, in general, quite complicated, but has nothing to do with the described task with delphi. @ ololo, no matter how you do, everything will be fine. The main thing is that it is correct for all input data and is clear to other programmers . - avp
  • Tell me how to add a code in the comments? - ololo
  • one
    @olo, you forgot about the prediction of transitions and the pre-selection of commands and data in modern CPUs, as well as the limitations of the size of the CPU buffers used for this. Here it is necessary to look very specifically at the CPU model, executable code and data. And it is better to consult, say, with Intel engineers. - avp
  • one
    > You just need to try to avoid unconditional "jumps". For Delphi - this is GOTO, for ASM - JMP. They are a bad tone, a bad programming style, and they also confuse. For the SED is true, for the assembler is not. In general, the assembler cycle consists of one conditional and one unconditional transition, so it is impossible to completely “avoid” JMP. You need to avoid spaghetti code , not JMP. - insolor
  • one
    Few things are clear, except for the fact of premature optimization. It is necessary to take a profiler and look for bottlenecks (which are probably far from in the binary code branching). - karmadro4

2 answers 2

You, it seems to me, approach the optimization from the wrong side.

Honestly, “but the code is very cumbersome” code in a loop is no longer a buzz. There is a rule that if an action can be done outside the loop, it needs to be done outside the loop.

Secondly. If your cycle did a billion repetitions, then it would be worthwhile to think about it. But you have 1000 repetitions - forget and hammer. In the most pessimistic forecast, it will run on a processor with at least 500 million cycles / second. To compare a register with a memory, you need 2 bars as I remember ...

Plus against the background of the "bulky" cycle - plus the CPU, minus the CPU. And how do you know what kind of ramming it will compile when))

    Want incredible performance? Then sorry, but you have chosen the wrong IDE. Delphi shoves a lot of unnecessary stuff into the executable file, and loads it heavily, thereby reducing performance and greatly increasing its size.

    Want performance in Delphi? Okay. Write programs using ONLY WinApi functions, not using VCL. Then yes, the executable file will give odds by the speed of exe's compiled into any other IDE, and will weigh several times less.

    you should try to avoid both conditional and unconditional transitions (goto and if .. then ..)

    Well, I still understand goto, but why didn’t you please the conditional operator? It actually helps to reduce the amount of code by doing metamorphic procedures / functions.

    could be combined into one, but the code is very cumbersome

    In any case, refactoring in the studio

    Ps. 1000 iterations alone is nonsense. It all depends on what is being done in them. So that you would at least roughly describe what you are doing, if of course you want a more or less distinct answer.

    • "Do you want performance in Delphi? Okay. Write programs using ONLY WinApi functions, not using VCL." For student coursework it can and does, in real life it is better not to do so. - alexlz
    • one
      @teanYCH @alexlz, what are you discussing? The question was: How many times slower is the processor making the transition to an arbitrary instruction compared to the normal transition to the next instruction? All the rest of the author is unknown for what he added. - avp
    • one
      @avp is a question like a clock question. The correct answer is: " Fuck knows" - alexlz