Question

I'm writting a series of anonymous functions for an objective-C project (i.e. these functions are not class specific / implementation is hidden) and I came across an interesting issue...

I have a macro function: div(c)((CGFloat)c/255.0f) This usage will almost always be something like div(0.0f), but others may not know that it takes a float so div(0) is possible

and the question I have is this: when variables are explicitly cast and the variable is of the same type as the cast is any performance lost to the cast?

Was it helpful?

Solution

A cast is a promise, not a data type, not a method, not an extension. You're just making the compiler comfortable about a type. Nothing should change execution-wise, therefore there is nothing to optimize execution-wise. If you're worried about the type of the parameter you've requested, you can always explicitly store it in a different CGFloat before operating on it.

OTHER TIPS

The machine works with memory. At the end all the variable that you use are just raw bytes.

Then why do Objective-C have types? To protect the programmer at compile time, showing errors and warnings.

At runtime all the operations that you do are made on the memory, so you don't need to worry about the overhead of casts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top