Length to Float in


I am doing some profiling of my code and I have noticed that the conversion from Length to Float takes around 12% of the total execution.

 %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 35.81   540.04    540.04   901584     0.60     4.45  Array#each
 12.46   727.90    187.86 31082436     0.01     0.01  Length#to_f
  3.24  1248.25     48.84 31099958     0.00     0.00  Float#to_f

The calculation can run for a few hours so this amount of time can become important.

Now. I know that SU uses inches internally and I have been quite careful (I think) in working only with internal units and exposing the model units only in the UI, but this conversion eludes me.

You can also notice that the number of calls to Length#to_f is almost exactly identical to Float#to_f (?) and this makes me suspicious.

I have read the ThomThom guide to units, but I could not find an answer to this.

Any idea?


Why do you think you need to convert Length objects to Float objects ?

As soon as you add, subtract, multiply or divide a Length object by any other Numeric subclass object, the result is a Float anyway. Even if the other object is a Length.

(On the C-side, Length are really Float type objects, that have overridden comparison methods, and some extra conversion methods.)

Are you explicitly calling Float#to_f in your code ? (Doing so serves no purpose. This might be happening “behind the curtain” because they are really float and are fooling the Ruby side.)


Thanks Dan.

No. The only values that I convert to Float are those that I read from external files. This morning I went through the code and insured that I was not not doing redundant conversions.

I understand that Length is a sub-class of Float and they should be almost the same things except for some specific methods like units conversions and geometric operations. So I imagined that once the conversion is done they would be treated as Float.

Say I ask the user for an input in meters. Internally I convert the input by doing

delta = input.m

and I continue all my calculations using delta. Does SU internally treats delta as a Length and for every operation makes a conversion to Float?