模板元编程

/*
 * @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;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海洋2416

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值