In C++, which is better i>

This question already has an answer here:

  • Is < faster than <=? 13 answers
  • x > -1 vs x >= 0, is there a performance difference 10 answers

  • In assembly language, both are on the same structure:

  • i > -1

    cmp   [register with i value],-1
    jg    [somewhere]
    
  • i >= 0

    cmp   [register with i value],0
    jge   [somewhere]
    
  • According to used jump flags, the instruction jg make two flags comparaisons (ZF = 0 and SF = OF) but jge does only one (SF = OF).

    So I'm tempted to say that both use almost same registers and CPU cycles, with maybe a very little quicker comparaison for i >= 0 .


    那么,根据逻辑>操作可能比“=便宜”,但我想你正在编译与优化选项启用,所以编译器可能会做他想优化你的代码,所以我会说这并不重要,即使其中一个速度更快,编译器可能会将其更改为最佳选项


    当编写for循环时,将它从for (i = 0; i < 1000; i++)转换for (i = 1000; i > 0; i--)会是有益的,因为在某些体系结构中,编译器可以跳过比较当我达到0时,作为标志的指令将被设置。在现代架构中,我不确定它是否重要。

    链接地址: http://www.djcxy.com/p/31552.html

    上一篇: 什么会更快,> =或>?

    下一篇: 在C ++中,这是更好的i>