1

I am doing research on data types and I started with INT and Float. I did a simple loop that has an equation inside it. The loop is first executed with int data type and then with float . The code is done in objective c but the idea is that they take the same time. However, while checking the instruments tool in the xcode, it seems that the float run more cycles on the CPU.
Can anybody explain please?

Sam
  • 86,580
  • 20
  • 181
  • 179
user1760556
  • 489
  • 1
  • 4
  • 4
  • What is your target? x86, ARM, something else? If your target doesn't support float natively, you might be using soft floats (which are very slow). – Patater Oct 19 '12 at 21:40
  • i am using xcode for iphone ARM – user1760556 Oct 19 '12 at 21:43
  • Can you post your code? Amongst other things, there could be some compiler optimizations that are taking place (such as throwing away the result if it's not used), etc.. – hexist Oct 19 '12 at 21:43
  • thanks hexist the code is only for loop and simple equation it it for (int i=0,i – user1760556 Oct 19 '12 at 21:46
  • This is probably a duplicate of http://stackoverflow.com/questions/2550281/floating-point-vs-integer-calculations-on-modern-hardware – Nikos C. Oct 19 '12 at 21:50
  • @user1760556 Any competent compiler will reduce that to x = i + 36000, and as n doesn't change inside the loop, an optimizing compiler could eleiminate the loop as well and just evaluate the final value of x at compile time. The test proves nothing. – user207421 Oct 19 '12 at 21:50

1 Answers1

2

Floating-point arithmetics is by far more complicated than integer arithmetics. Usually, CPUs even have a dedicated FPU circuitry to perform floating-point operations. Thus, what you have observed is to be expected.