Sunday, February 10, 2013

Can I stop typing "f" now?

Can I please stop worrying about "f" now? I have to admit I've never worried about it too much, but let's see if that was foolish on my part.

First test is simple assignment:





Let's see how the compiler deals with these two lines:








This is great! The compiler knew that I wanted floats, and that's the literals that it made. Thanks compiler! We don't have to type an f!

Now let's try to make things more interesting. What if I make the result a float, and divide an int by a literal? And I'll put it in a billion-cycle loop just for fun:








Here is a diff of the compiled assembly:









On the left is where I didn't put an f after the 3.2 literal. The compiler converted the integer i into a double. Then it did a double division. Then because result is a float, it had to convert the result from a double to a float (single).

On the right is where I did put an f after the 3.2 literal. The compiler had to convert the integer i into a float (single) this time instead of a double, but then no additional conversion was required because the result of the division was already a float to put into the result variable.

So unlike the first example of a simple literal assignment, there is an extra runtime conversion in this example. That is going to cost us, but how much? You saw I did a billion of them, and I ran it on an iPhone 5. Here is a typical result:




That's 0.00000001549739 seconds per conversion. It's going to be hard for me to worry about f too much unless I'm doing something a billion or OK, maybe a million times.

Or really now that I think about it, a million times isn't enough times to worry about it:




0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home