Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Compile and link:
- // g++ -std=c++11 -O3 -march=native main.cpp -lm
- // Run and benchmark:
- // echo '1234 567890' > cin.txt
- // time ./a.out < cin.txt
- // Check compiler optimizations:
- // objdump -dC -j .text a.out > disasm.s
- // Use static test values, or allow user input from cin?
- // 0: Use two integers read from cin.
- // 1: Use const values 1234 and 567890.
- #define STATIC 0
- // See also the commented out lines in main() below!!!
- #include <iostream>
- #include <sstream>
- #include <cmath>
- #include <cstdlib>
- namespace i2f {
- namespace constant {
- namespace {
- template <typename F=double, typename I=long>
- F scale(const F post) {
- return static_cast<I>(post) ? scale(post * 0.1) : post;
- }
- } // helper
- template <typename F=double, typename I=long>
- constexpr F concat(const I &pre, const I &post) {
- return static_cast<F>(pre) + scale<F>(post);
- }
- } // namespace constant
- namespace log {
- template <typename F=double, typename I=long>
- inline F concat(const I &pre, const I &post) {
- return pre + (post > 0 ? (post / ::pow(10, ::floor(::log10(post)) + 1)) : 0);
- }
- } // namespace log
- namespace mul {
- template <typename F=double, typename I=long>
- inline F concat(const I &pre, const I &post) {
- F frac = post;
- while (frac >= 1) {
- frac *= 0.1;
- }
- return pre + frac;
- }
- } // namespace mul
- namespace str {
- namespace {
- template <typename F>
- inline F parse_float(const char *cstr) = delete;
- template <>
- inline float parse_float<float>(const char *cstr) {
- return strtof(cstr, nullptr);
- }
- template <>
- inline double parse_float<double>(const char *cstr) {
- return strtod(cstr, nullptr);
- }
- template <>
- inline long double parse_float<long double>(const char *cstr) {
- return strtold(cstr, nullptr);
- }
- } // helper
- namespace a {
- template <typename F=double, typename I=long>
- inline F concat(const I &pre, const I &post) {
- using namespace std;
- stringstream ss;
- ss << pre << '.' << post;
- constexpr size_t bufsz = 128;
- char buf[bufsz];
- ss.get(buf, bufsz);
- return parse_float<F>(buf);
- }
- } // namespace a
- namespace b {
- template <typename F=double, typename I=long>
- inline F concat(const I &pre, const I &post) {
- using namespace std;
- stringstream ss;
- ss << post;
- constexpr I base = 10;
- const I exp = static_cast<I>(ss.tellp());
- return pre + post / ::pow(base, exp);
- }
- } // namespace b
- } // namespace str
- } // namespace i2f
- int main(void) {
- using namespace std;
- #if STATIC == 0
- cout << "DYNAMIC mode!!!" << endl;
- long i1, i2;
- cout << "Enter two integers separated by space:" << endl;
- cin >> i1 >> i2;
- # else
- cout << "STATIC mode!!!" << endl;
- const long i1 = 1234;
- const long i2 = 567890;
- # endif
- cout << "Using " << i1 << " and " << i2 << '.' << endl;
- constexpr size_t count = 10000000L;
- cout << "Iterating " << count << " times ..." << endl;
- double d = 0.0;
- // The "volatile" keyword prevents the compiler from
- // completely removing the following loop, while keeping
- // all other optimizations intact.
- for (volatile size_t i = 0; i < count; ++i) {
- //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- //XXX Uncomment only a single line per test!
- //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- // static 0.01 / dynamic 0.04
- d = i2f::constant::concat(i1, i2);
- // static 0.01 / dynamic 1.00
- //d = i2f::log::concat(i1, i2);
- // static 0.01 / dynamic 0.04
- //d = i2f::mul::concat(i1, i2);
- // static 8.66 / dynamic 8.66
- //d = i2f::str::a::concat(i1, i2);
- // static 6.52 / dynamic 6.45
- //d = i2f::str::b::concat(i1, i2);
- }
- // Result as float: 1234.567890
- cout << fixed << "Concatenated result: " << d << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement