• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            qiezi的學習園地

            AS/C/C++/D/Java/JS/Python/Ruby

              C++博客 :: 首頁 :: 新隨筆 ::  ::  :: 管理 ::
            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<doublefloat>
            {
                typedef 
            double BigType;
            };

            template 
            <>
            struct Type<floatdouble>
            {
                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(
            12)) == typeid(int));
                assert (MaX(
            12== 2);

                assert (typeid(MaX(
            12.5)) == typeid(double));
                assert (MaX(
            12.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;
            }
            posted on 2005-12-12 10:45 qiezi 閱讀(2749) 評論(7)  編輯 收藏 引用 所屬分類: C++
            日本精品久久久中文字幕| 国产精自产拍久久久久久蜜| 久久婷婷五月综合成人D啪| 熟妇人妻久久中文字幕| 久久免费线看线看| 人人妻久久人人澡人人爽人人精品 | 亚洲AV无码久久精品色欲| 久久综合综合久久97色| 久久精品国产免费观看三人同眠| 久久国产欧美日韩精品| 久久亚洲国产成人影院网站| 精品久久久久久亚洲精品 | 色妞色综合久久夜夜| 狠狠精品干练久久久无码中文字幕| 久久精品国产99国产精品导航| 亚洲国产精久久久久久久| 精品久久久久香蕉网| 久久久久久久久久久精品尤物| 久久久久人妻一区精品果冻| 无遮挡粉嫩小泬久久久久久久| 合区精品久久久中文字幕一区| 国产精品无码久久久久| 天天爽天天爽天天片a久久网| 色综合久久综合中文综合网| 中文字幕无码久久人妻| 久久综合伊人77777麻豆| 狠狠人妻久久久久久综合| 国产精品一区二区久久精品| 久久国产精品无码HDAV| 精品久久久久久亚洲精品| 久久久久99精品成人片直播| 久久国产欧美日韩精品| 伊人久久精品无码二区麻豆| 成人午夜精品无码区久久| 亚洲精品乱码久久久久久| 欧美亚洲色综久久精品国产| 久久亚洲AV成人无码国产| 精品国产VA久久久久久久冰 | 少妇精品久久久一区二区三区| 伊人色综合久久天天人手人婷| 亚洲精品乱码久久久久久久久久久久 |