github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/benchmark/common/math.cpp (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  #include "common/math.h"
    21  #include "benchmark/benchmark_instances.h"
    22  #include <cmath>
    23  #include <iostream>
    24  
    25  BENCHMARK(math_benchamrk_plus, softfloat_plus) {
    26    // exp
    27    auto e1 = neb::math::exp(float64(1));
    28    auto e2 = neb::math::exp(float64(2));
    29    for (int i = -9000; i < 9000; ++i) {
    30      e1 = e1 + e2;
    31    }
    32  }
    33  BENCHMARK(math_benchamrk_plus, std_plus) {
    34    // exp
    35    auto e1 = std::exp(1);
    36    auto e2 = std::exp(2);
    37    for (int i = -9000; i < 9000; ++i) {
    38      e1 = e1 + e2;
    39    }
    40  }
    41  BENCHMARK(math_benchamrk_plus1, softfloat_plus1) {
    42    // exp
    43    auto e1 = neb::math::exp(float64(1));
    44    auto e2 = neb::math::exp(float64(2));
    45    for (int i = -9000; i < 9000; ++i) {
    46      e1 = e1 - e2;
    47    }
    48  }
    49  BENCHMARK(math_benchamrk_plus1, std_plus1) {
    50    // exp
    51    auto e1 = std::exp(1);
    52    auto e2 = std::exp(2);
    53    for (int i = -9000; i < 9000; ++i) {
    54      e1 = e1 - e2;
    55    }
    56  }
    57  
    58  BENCHMARK(math_benchamrk_mul1, softfloat_mul1) {
    59    // exp
    60    auto e1 = neb::math::exp(float64(1));
    61    auto e2 = neb::math::exp(float64(2));
    62    for (int i = -9000; i < 9000; ++i) {
    63      e1 = e1 * e2;
    64    }
    65  }
    66  
    67  BENCHMARK(math_benchamrk_mul1, std_mul1) {
    68    // exp
    69    auto e1 = std::exp(1);
    70    auto e2 = std::exp(2);
    71    for (int i = -9000; i < 9000; ++i) {
    72      e1 = e1 * e2;
    73    }
    74  }
    75  
    76  BENCHMARK(math_benchamrk_div1, softfloat_div1) {
    77    // exp
    78    auto e1 = neb::math::exp(float64(1));
    79    auto e2 = neb::math::exp(float64(2));
    80    for (int i = -9000; i < 9000; ++i) {
    81      e1 = e1 / e2;
    82    }
    83  }
    84  BENCHMARK(math_benchamrk_div1, std_div1) {
    85    // exp
    86    auto e1 = std::exp(1);
    87    auto e2 = std::exp(2);
    88    for (int i = -9000; i < 9000; ++i) {
    89      e1 = e1 / e2;
    90    }
    91  }
    92  
    93  BENCHMARK(math_benchamrk_exp, softfloat_exp) {
    94    // exp
    95    for (int i = -9000; i < 9000; ++i) {
    96      auto e = neb::math::exp(float64(10));
    97      std::cout << "neb::exp(" << 10 << "): " << e << std::endl;
    98    }
    99  }
   100  BENCHMARK(math_benchamrk_exp, std_exp) {
   101    // exp
   102    for (int i = -9000; i < 9000; ++i) {
   103      auto e = std::exp(10);
   104      std::cout << "std::exp(" << 10 << "): " << e << std::endl;
   105    }
   106  }
   107  
   108  BENCHMARK(math_benchamrk_arctan, softfloat_arctan) {
   109    // arctan
   110    for (int i = -9000; i < 9000; ++i) {
   111      auto pi = neb::math::arctan(float64(10));
   112      std::cout << "neb::arctan(" << 10 << "): " << pi << std::endl;
   113    }
   114  }
   115  BENCHMARK(math_benchamrk_arctan, std_arctan) {
   116    // arctan
   117    for (int i = -9000; i < 9000; ++i) {
   118      auto pi = std::atan(10);
   119      std::cout << "std::atan(" << 10 << "): " << pi << std::endl;
   120    }
   121  }
   122  
   123  BENCHMARK(math_benchamrk_sin, softfloat_sin) {
   124    // sin
   125    for (int i = -9000; i < 9000; i++) {
   126      auto s = neb::math::sin(float64(10));
   127      std::cout << "neb::sin(" << 10 << "): " << s << std::endl;
   128    }
   129  }
   130  BENCHMARK(math_benchamrk_sin, std_sin) {
   131    // sin
   132    for (int i = -9000; i < 9000; i++) {
   133      auto s = std::sin(10);
   134      std::cout << "std::sin(" << 10 << "): " << s << std::endl;
   135    }
   136  }
   137  
   138  BENCHMARK(math_benchamrk_ln, softfloat_ln) {
   139    // ln
   140    for (int i = 1; i < 10000; ++i) {
   141      auto l = neb::math::ln(float64(10));
   142      std::cout << "neb::ln(" << 10 << "): " << l << std::endl;
   143    }
   144  }
   145  BENCHMARK(math_benchamrk_ln, std_ln) {
   146    // ln
   147    for (int i = 1; i < 10000; ++i) {
   148      auto l = std::log(10);
   149      std::cout << "std::log(" << 10 << "): " << l << std::endl;
   150    }
   151  }
   152  
   153  BENCHMARK(math_benchamrk_log2, softfloat_log2) {
   154    // log2
   155    for (int i = 1; i < 10000; ++i) {
   156      auto l2 = neb::math::log2(float64(10));
   157      std::cout << "neb::log2(" << 10 << "): " << l2 << std::endl;
   158    }
   159  }
   160  BENCHMARK(math_benchamrk_log2, std_log2) {
   161    // log2
   162    for (int i = 1; i < 10000; ++i) {
   163      auto l2 = std::log2(10);
   164      std::cout << "std::log2(" << 10 << "): " << l2 << std::endl;
   165    }
   166  }
   167  
   168  BENCHMARK(math_benchamrk_pow, softfloat_pow) {
   169    // pow
   170    auto e = neb::math::constants<float64>::e();
   171    for (int i = -90; i < 90; ++i) {
   172      auto p = neb::math::pow(e, float64(10));
   173      std::cout << "neb::pow(e, " << 10 << "): " << p << std::endl;
   174    }
   175  }
   176  BENCHMARK(math_benchamrk_pow, std_pow) {
   177    // pow
   178    auto e = std::exp(1);
   179    for (int i = -9000; i < 9000; ++i) {
   180      auto p = std::pow(e, 10);
   181      std::cout << "std::pow(e, " << 10 << "): " << p << std::endl;
   182    }
   183  }