C++ (Qt)#include <iostream>#include <fstream>#include <chrono> #include <specmath/roots.h> template <class T>T exact_func(const T & x, const T & err = 0.000001){ static const T e = exp(1); const T ln_x = log(x); const T min = (x < e) ? 0 : 1; const T max = (x < e) ? 1 : ln_x; return specmath::bisect([&](const T& s) { return (x < e) ? (x - s * exp(s)) : (ln_x - s - log(s)); }, min, max, err);} template <class T>T approx_func(const T & x){ return log(1 + x)/1.35;} int main(){ double x = 0; const double xmax = 10000; const double dx = 1; std::ofstream out1("data1.txt"); std::ofstream out2("data2.txt"); auto start = std::chrono::high_resolution_clock::now(); while (x < xmax) { out1 << x << " " << exact_func(x) << std::endl; x += dx; } auto stop = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(stop-start).count(); std::cout << " bisection: total time = " << elapsed << "ms" << std::endl; x = 0; start = std::chrono::high_resolution_clock::now(); while (x < xmax) { out2 << x << " " << approx_func2(x) << std::endl; x += dx; } stop = std::chrono::high_resolution_clock::now(); elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(stop-start).count(); std::cout << "aprox func: total time = " << elapsed << "ms" << std::endl; return 0;}
Bash bisection: total time = 58msaprox func: total time = 50ms bisection: total time = 58msaprox func: total time = 50ms bisection: total time = 58msaprox func: total time = 51ms