Giving attention to types is something of utmost importance in programming, especially in C++.
If float
it was useless it did not exist, right? It is very useful and I hope it is not yet a C++ programmer, where it is too useful. A double
takes up twice as much space and in many cases this makes a huge difference. Moreover depending on the type of platform a double
may be slower by having to do two-part operations. Of course double
may be faster on some platform, but this doesn’t always happen even when it’s 64 bits. Talking generically about this is useless, what counts is the test proving that it is faster in the situation you need.
If the unsigned int
accelerates the split also depends on the platform where it is running, the ideal is to use the most suitable type and almost always the int` is good. Unsigned types are more difficult to understand and have unexpected behaviors in some situations, so one should avoid. It can be used smoothly, there is no such thing as never to be used, it should only be avoided if it is not absolutely necessary (there is issues to be observed).
If an API you use has this type you have to work with this type, you have no choice, at least directly, of course in some cases you can take the data like this and turn into int
if desired and useful to do this (there may be a conversion that has a cost).
Only do optimizations of this type if you have a performance problem and know where to move. In addition you should measure to see if you can perform better, you may not be able to, in many cases you will not. And there are other ways to optimize this. Division really is slow, and it has effective techniques to optimize this in many cases.
The right choice is a complete mastery of everything about computing, the language, the compiler you use, the platform you run and experience a lot. There is no magic answer.
The use of auto
it is not for you not to think about the type you are using, quite the contrary, it is to say that the type does not matter in that case, which is not always what you want. auto
It is not to save typing or accept any type, it is to say that the type that comes from the expression being assigned in the statement is acceptable, and even if it changes to your code it is all ok. You thought about the type and established that it doesn’t matter.
If unsigned does not meet the requirements of the application there is no reason to use it. The choice of data type should take account of the application requirements as a priority. Remember that early optimization is the hand of all evils.
– anonimo