Is extra time required for float vs int comparisons?

If you have a floating point number double num_float = 5.0; and the following two conditionals.

if(num_float > 3)
{
    //...
}

if(num_float > 3.0)
{
    //...
}

Q: Would it be slower to perform the former comparison because of the conversion of 3 to a floating point, or would there really be no difference at all?

Obviously I'm assuming the time delay would be negligible at best, but compounded in a while(1) loop I suppose over the long run a decent chunk of time could be lost (if it really is slower).


Because of the "as-if" rule, the compiler is allowed to do the conversion of the literal to a floating point value at compile time. A good compiler will do so if that results in better code.

In order to answer your question definitively for your compiler and your target platform(s), you'd need to check what the compiler emits, and how it performs. However, I'd be surprised if any mainstream compiler did not turn either of the two if statements into the most efficient code possible.


If the value is a constant, then there shouldn't be any difference, since the compiler will convert the constant to float as part of the compilation [unless the compiler decides to use a "compare float with integer" instruction].

If the value is an integer VARIABLE, then there will be an extra instruction to convert the integer value to a floating point [again, unless the compiler can use a "compare float with integer" instruction].

How much, if any, time that adds to the whole process depends HIGHLY on what processor, how the floating point instructions work, etc, etc.

As with anything where performance really matters, measure the alternatives. Preferably on more than one type of hardware (eg both AMD and Intel processors if it's a PC), and then decide which is the better choice. Otherwise, you may find yourself tuning the code to work well on YOUR hardware, but worse on some other hardware. Which isn't a good optimisation - unless the ONLY machine you ever run on is your own.


Note: This will need to be repeated with your target hardware. The code below just demonstrates nicely what has been said.


with constants:

bool with_int(const double num_float) {
  return num_float > 3;
}

bool with_float(const double num_float) {
  return num_float > 3.0;
}

g++ 4.7.2 ( -O3 -march=native ):

with_int(double):
  ucomisd       .LC0(%rip), %xmm0
  seta  %al
  ret
with_float(double):
  ucomisd       .LC0(%rip), %xmm0
  seta  %al
  ret
.LC0:
  .long 0
  .long 1074266112

clang 3.0 ( -O3 -march=native ):

.LCPI0_0:
  .quad 4613937818241073152     # double 3.000000e+00
with_int(double):                           # @with_int(double)
  ucomisd       .LCPI0_0(%rip), %xmm0
  seta  %al
  ret

.LCPI1_0:
  .quad 4613937818241073152     # double 3.000000e+00
with_float(double):                        # @with_float(double)
  ucomisd       .LCPI1_0(%rip), %xmm0
  seta  %al
  ret

Conclusion: No difference if comparing against constants.


with variables:

bool with_int(const double a, const int b) {
  return a > b;
}

bool with_float(const double a, const float b) {
  return a > b;
}

g++ 4.7.2 ( -O3 -march=native ):

with_int(double, int):
  cvtsi2sd      %edi, %xmm1
  ucomisd       %xmm1, %xmm0
  seta  %al
  ret
with_float(double, float):
  unpcklps      %xmm1, %xmm1
  cvtps2pd      %xmm1, %xmm1
  ucomisd       %xmm1, %xmm0
  seta  %al
  ret

clang 3.0 ( -O3 -march=native ):

with_int(double, int):                          # @with_int(double, int)
  cvtsi2sd      %edi, %xmm1
  ucomisd       %xmm1, %xmm0
  seta  %al
  ret

with_float(double, float):                       # @with_float(double, float)
  cvtss2sd      %xmm1, %xmm1
  ucomisd       %xmm1, %xmm0
  seta  %al
  ret

Conclusion: the emitted instructions differ when comparing against variables, as Mats Peterson's answer already explained.

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

上一篇: 在R中花车少或相等

下一篇: float和int比较需要额外的时间吗?