Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much performance gain for changing variables from double to float? [duplicate]

I have a program that takes too much time, so I want to optimize my code a bit.

I have used the double type for every variable so far. If I change to be of type float, will any performance benefits occur?

like image 905
taha Avatar asked Sep 15 '25 11:09

taha


2 Answers

It is impossible to answer this question with any certainty: it will depend on your code and your hardware. The change will have many possible effects:

  • Memory usage will be reduced.
  • Cache misses will be fewer.
  • CPU instructions will take fewer cycles.
  • The compiler may autovectorize, or autovectorize differently.
  • Numerical algorithms in your application may no longer converge correctly.

The only way to tell the actual performance difference is to test it yourself. Sounds like a simple search & replace job.

like image 57
Dietrich Epp Avatar answered Sep 17 '25 01:09

Dietrich Epp


Most likely, you will only see noticeable improvements if your code works on a very large block of memory. If you are doing double operations on an array of millions of values, you'll cut your memory bandwidth in half by switching to float. (I'm assuming you are on a standard architecture where float is 32 bits and double is 64 bits.)

In terms of reducing load on the CPU, I wouldn't expect to see a significant change. Maybe a small difference for some operations, but probably a few percent at best.

like image 42
StilesCrisis Avatar answered Sep 17 '25 00:09

StilesCrisis