0

I was told that if == computes faster than if < or if > by a senior student at my school during IT extra classes. Is this true?

adamaero
  • 220
  • 3
  • 17
turoo
  • 127
  • 4
  • 1
    no... not at a language level for plain old data... there may be some implementations where it does, but that is way below the level of C++. – Grady Player Aug 21 '19 at 14:09
  • 1
    It depends. For example, it might be true for strings, where check for equality may first compare string sizes and immediately return `false` if they do not match. Lexicographical comparison might involve more operations, e.g., if compared strings start with same character sequences. You should specify the type of data. – Daniel Langr Aug 21 '19 at 14:11
  • 9
    In my experience as a student, senior students are among the most unreliable sources of information you can find, regularly mistaking their beliefs for knowledge. – molbdnilo Aug 21 '19 at 14:11
  • @BenVoigt Vote for reopening, since this question does not mention comparison of integers. However, OP should specify the type of data. Otherwise, I would close it as too broad. – Daniel Langr Aug 21 '19 at 14:16
  • @DanielLangr The answer is the same, though. To this and any other similar question that could be asked. – dandan78 Aug 21 '19 at 14:18
  • 1
    @DanielLangr: OP needs to understand the case for integers (which includes pointers) and then if his case is different, he'll be able to edit his question into something clear about his unique situation. With the question as it stands, no further answer than the one about integers is possible. – Ben Voigt Aug 21 '19 at 14:18
  • 1
    While having a lot of seniority may seem to imply that they know what they are talking about, it's important to know that this same seniority can suggest that they trained and worked with knowledge and technology that is now outdated. As much as experience matters, keeping informed and up to date is just as important. Otherwise, the relevance of that experience decays with time (and technology evolves quickly). Edit : This goes doubly for academics, whose work tends to be narrower or more theoretical. – François Andrieux Aug 21 '19 at 14:20
  • In most processors, comparing is performed by subtraction. During the subtraction process, flags are set, such as negative result, equal or zero result, etc. The program can then use a conditional jump instruction. Down to the assembly language, there is no difference in speed between an equality comparison and a greater than or less than comparison. At the microcode level, there may be, but the difference would be negligible (like on the order of nanoseconds). – Thomas Matthews Aug 21 '19 at 18:32
  • For equality, you could use an XOR instruction, but you still have to perform a conditional branch (unless your processor has conditional execution of instructions). Either way, you still need some type of comparison instruction or instruction to set condition flags. – Thomas Matthews Aug 21 '19 at 18:34

0 Answers0