Noah Roberts wrote:
I understand that different integer types might have time penalties
because of word boundaries. Where can I get information clarifying
this topic? A quick google didn't turn up much for me...nice little
C++ vs. Java troll war on wikipedia but nothing on what I'm looking
for. I want to know whether space savings using smaller types to
represent numbers you know will be small are ever seen. I would also
like to know what kind of performance penalties are associated with
using these smaller types.
Performance penalties depends on the context and the platform. A
primitive type has a range, a min and max value as well as a few more
interesting constants. You can dissect the various qualities of a
primitive using the <limitsheader. Note that performance = 0 if the
primitive you choose generates an overflow. So before you get concerned
with speed, consider the ramifications.
#include <limits>
template< typename T >
void Numerics( const T& r_t )
{
std::cout << "\ntype: " << typeid(r_t).name();
bool is_int = std::numeric_limits< T >::is_integer;
std::cout << "\ninteger = " << is_int;
bool is_signed = std::numeric_limits< T >::is_signed;
std::cout << "\tsigned = " << is_signed;
std::cout << "\tsizeof = " << sizeof(r_t);
T min = std::numeric_limits< T >::min();
std::cout << "\nmin = " << min;
T max = std::numeric_limits< T >::max();
std::cout << "\nmax = " << max;
long capacity = static_cast<long>(max) - min;
std::cout << "\ncapacity = " << capacity;
std::cout << std::endl;
}
int main()
{
int n(0);
Numerics(n);
// etc
}