c++ - Cost of conversion between integral types -
i creating supplemental library of math functions use in programs, , wish implement gcd function. may end using function frequently, optimization extremely important.
i wondering if, regard optimization, there point in implementing several gcd overloads different integral types. illustration:
int gcd(const int lhs, const int rhs); long gcd(const long lhs, const long rhs); long long gcd(const long long lhs, const long long rhs);
is there inherent cost when converting between integral types, or can implement long long
version , call "good enough"?
well, first of all, can template it:
template <typename t> t gcd(const t lhs, const t rhs);
and call gcd<int>(a, b)
or whichever type need, if primary concern code duplication.
that said, while doubt integral type conversion ever show in profiling, sort of thing should first write in convenient way , worry optimizing if , when have optimize it.
Comments
Post a Comment