CSDN上看到有人问能否实现一个效率较高的max函数,效率接近于宏,于是自动动手写了一个。
由于max宏在判断不同类型时,能够返回大的那个类型(表示范围大),所以只能使用模板来进行返回类型的推导。
在VC8上打开O2或Ox优化选项,测试结果是与宏效率相等。
全部实现如下:
#include <typeinfo>
#include <cassert>
#include <windows.h>
#include <iostream>
template <class T, class U, bool B>
struct BigType
{
typedef T result;
};
template <class T, class U>
struct BigType<T, U, false>
{
typedef U result;
};
template <class T, class U>
struct Type
{
typedef typename BigType<T, U, (sizeof(T) > sizeof(U))>::result BigType;
};
template <class T>
struct Type<T, double>
{
typedef double BigType;
};
template <class T>
struct Type<double, T>
{
typedef double BigType;
};
template <class T>
struct Type<T, float>
{
typedef float BigType;
};
template <class T>
struct Type<float, T>
{
typedef float BigType;
};
template <>
struct Type<double, float>
{
typedef double BigType;
};
template <>
struct Type<float, double>
{
typedef double BigType;
};
template <class T, class U>
typename Type<T, U>::BigType MaX (const T& t, const U& u)
{
typedef typename Type<T, U>::BigType ResultType;
return ResultType(t > u ? t : u); // 原为return (ResultType)t > u ? t : u;
}
int main ()
{
assert (typeid(MaX(1, 2)) == typeid(int));
assert (MaX(1, 2) == 2);
assert (typeid(MaX(1, 2.5)) == typeid(double));
assert (MaX(1, 2.5) == 2.5);
assert (typeid(MaX(1, (float)2.5)) == typeid(float));
assert (MaX(1, (float)2.5) == 2.5);
assert (typeid(MaX((double)2, (float)2.5)) == typeid(double));
assert (MaX((double)2, (float)2.5) == 2.5);
assert (typeid(MaX((long)2, (float)2.5)) == typeid(float));
assert (MaX((long)2, (float)2.5) == 2.5);
assert (typeid(MaX((long)2, (short)2)) == typeid(long));
assert (MaX((long)2, (short)2) == 2);
assert (typeid(MaX((float)2, (__int64)2)) == typeid(float));
assert (MaX((float)2, (__int64)2) == 2);
assert (std::string("hello") < "world");
assert (typeid(MaX(std::string("hello"), "world")) == typeid(std::string));
assert (MaX(std::string("hello"), "world") == "world");
assert (typeid(MaX(std::string("world"), "hello")) == typeid(std::string));
assert (MaX(std::string("hello"), "world") == "world");
// 测试数,需定义在循环外,防止编译器优化掉无意义的循环
__int64 test = 0;
long start = GetTickCount();
for (int i=0; i<1000000000; ++i)
{
test += MaX(i, (__int64)i);
}
// test必须被使用,否则编译器视为无用数据,会被优化掉
std::cout << test << std::endl;
std::cout << (GetTickCount() - start) << std::endl;
test = 0;
start = GetTickCount();
for (int i=0; i<1000000000; ++i)
{
test += max(i, (__int64)i);
}
std::cout << test << std::endl;
std::cout << (GetTickCount() - start) << std::endl;
return 0;
}
相关推荐
C语言程序设计-编写函数实现两个数据的交换,在主函数中输入任意三个数据,调用函数对这三个数据从大到小排序;.c
max函数求4个数最大值
本文实例讲述了Python中max函数用法。分享给大家供大家参考。具体如下: 这里max函数是Python内置的函数,不需要导入math模块 # 最简单的 max(1, 2) max('a', 'b') # 也可以对列表和元组使用 max([1,2]) max((1,2))...
linux内核中的min、max函数1
max函数.xls
对于lena图像的量化,以及利用Lloyd Max函数量化,有文档,有代码,运行通过的,平台式matlab!
编写自定义函数max,用来求三个数的最大值(要求:数据的输入、输出在主函数中实现)。
c++ max函数
定义一个数getMax()函数,返回三个数(从键盘输入的整数)中最大值。 def getMax(): num1=int(input("请在键盘上输入第1个整数:")) num2=int(input("请在键盘上输入第2个整数:")) num3=int(input("请在键盘上...
辅以控制软件和特别的外围电路设计,就能实现一个低成本、多功能、高精度、输出频率连续可调的频率合成式波形发生器。并对其振荡频率控制、信号输出幅度控制、频率和幅度的控制作了比较详细的论述。最后根据MAX038的...
TIA博途中如何使用MAX和MIN数学函数求取几个变量的最大值和最小值?
利用Excel MAX函数或SUBTOTAL函数取得各科成绩的最高分.rar,有一份期末考试成绩表,要求计算出各科成绩的最高分,使用MAX函数或SUBTOTAL函数可以实现之。
(2)编写一个函数把华氏温度转换为摄氏温度,转换公式为C= (F一32)*5/9。 (3)编写重载函数Maxl可分别求取两个整数,三个整数,两个双精度数,三个双精度数的最大值。 (4)使用重载函数模板重新实现上小题中的...
c语言求最大值max函数
excel max函数如何使用.docx
1、本文详细描述了C++语言max函数的介绍及使用方法。 2、通过详细示例,让读者更直观地阅读,更清晰的理解。 3、示例代码可直接复制,编译后可直接运行。 4、根据示例以及运行结果,让读者加强记忆及理解。
细说matlab中的max函数.docx
MAX038函数信号发生器设计,应用电路,
利用python进行求解,求解的要求是不能使用python内部封装好的函数例如:max way1: def findmax(data,n): if n==1: return data[0] else: maxi=data[0] for i in data[1:]: if maxi<i: maxi=i return ...
JavaScript的Math对象带有一个max函数用于获取两个数字的较大数,下面的代码详细演示了max的用法 <!DOCTYPE html> <html> <body> Click the button to return the highest number of 5 and 10. &...