/*
* @Description:
* @Version: 1.0
* @Author: hanhy
*/
#include <chrono>
#include <cmath>
#include <cstdint>
#include <iostream>
using namespace std;
// =============阶乘====================
// 1
template<uint64_t N> class Fact {
public:
enum { Value = N * Fact<N - 1>::Value };
};
template<> class Fact<1> {
public:
enum { Value = 1 };
};
// 2
uint64_t FactFun(uint64_t N) {
if (N == 1)
return 1;
else
return N * FactFun(N - 1);
}
// 3
constexpr uint64_t ConstFactFun(uint64_t N) {
if (N == 1)
return 1;
else
return N * ConstFactFun(N - 1);
}
// =============计算斐波那契数====================
// 1
template<uint64_t N> class Fib {
public:
enum { Value = Fib<N - 1>::Value + Fib<N - 2>::Value };
};
template<> class Fib<1> {
public:
enum { Value = 1 };
};
template<> class Fib<2> {
public:
enum { Value = 1 };
};
// 2
uint64_t FibFun(uint64_t N) {
if (N == 1)
return 1;
else if (N == 2)
return 1;
else
return FibFun(N - 1) + FibFun(N - 2);
}
// 3
constexpr uint64_t ConstFibFun(uint64_t N) {
if (N == 1)
return 1;
else if (N == 2)
return 1;
else
return ConstFibFun(N - 1) + ConstFibFun(N - 2);
}
// =============计算Pow函数====================
// 1
template<uint64_t base, uint64_t exp> class Pow {
public:
enum { Value = base * Pow<base, exp - 1>::Value };
};
template<uint64_t base> class Pow<base, 1> {
public:
enum { Value = base };
};
// 2
uint64_t PowFun(uint64_t base, uint64_t exp) {
if (exp == 1)
return base;
else
return base * PowFun(base, exp - 1);
}
constexpr uint64_t ConstPowFun(uint64_t base, uint64_t exp) {
if (exp == 1)
return base;
else
return base * ConstPowFun(base, exp - 1);
}
int main() {
std::cout << "=============阶乘====================" << std::endl;
// 1
auto start11 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
auto v11 = Fact<25>::Value;
auto end11 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "v11:" << v11 << " used time:" << (end11 - start11) << std::endl;
// 2
auto start12 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
auto v12 = FactFun(25);
auto end12 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "v12:" << v12 << " used time:" << (end12 - start12) << std::endl;
// 3
auto start13 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
constexpr uint64_t v13 = ConstFactFun(25);
auto end13 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "v13:" << v13 << " used time:" << (end13 - start13) << std::endl;
// 4
auto start14 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
uint64_t v14 = ConstFactFun(25);
auto end14 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "v14:" << v14 << " used time:" << (end14 - start14) << std::endl;
std::cout << "=============斐波那契====================" << std::endl;
// 1
auto start21 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
auto v21 = Fib<40>::Value;
auto end21 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "v21:" << v21 << " used time:" << (end21 - start21) << std::endl;
// 2
auto start22 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
auto v22= FibFun(40);
auto end22 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "v22:" << v22 << " used time:" << (end22 - start22) << std::endl;
// 3
auto start23 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
auto v23 = ConstFibFun(40);
auto end23 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "v23:" << v23 << " used time:" << (end23 - start23) << std::endl;
std::cout << "=============Pow====================" << std::endl;
// 1
auto start31 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
auto v31 = Pow<2, 15>::Value;
auto end31 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "v31:" << v31 << " used time:" << (end31 - start31) << std::endl;
// 2
auto start32 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
auto v32 = std::pow(2, 15);
auto end32 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "v32:" << v32 << " used time:" << (end32 - start32) << std::endl;
// 3
auto start33 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
auto v33 = PowFun(2, 15);
auto end33 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "v33:" << v33 << " used time:" << (end33 - start33) << std::endl;
// 4
auto start34 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
constexpr uint64_t v34 = ConstPowFun(2, 15);
auto end34 =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "v34:" << v33 << " used time:" << (end34 - start34) << std::endl;
}
12-28
1643
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)