• <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>
            posts - 28, comments - 179, trackbacks - 0, articles - 1
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            MSVC8中的SafeCode對性能的影響

            Posted on 2007-06-11 10:51 chemz 閱讀(2092) 評論(4)  編輯 收藏 引用 所屬分類: C++
                                      MSVC8中的SafeCode對性能的影響
                由于前面一篇關于編譯boost1.34.0編譯的blog
            http://www.shnenglu.com/chemz/archive/2007/06/06/25666.html
            中有人提到在Visual C++ 2005中由于微軟采用了safe code技術會對性能產生非常大的影
            響(下降50%),所以本著性能不可以通過估計和猜測來確定,所以編寫了一個針對于此的
            測試程序進行了測試。
                該測試程序為了做到足夠的覆蓋C++標準庫中STL部分,所以選擇了容器/跌代器/算法三
            個關鍵組成中的具有代表性的類和對象進行了測試。首先解釋一下微軟所提供的safe code
            的目的和作用,由于STL庫中為了考慮到性能的最大化所以對于像:邊界值檢查、合法性檢
            查以及范圍檢查等都省略了,此部分需要依靠程序員自行處理,而safe code就是在STL設計
            到此部分的代碼中添加了判斷用的語句(通過宏引入的)。究竟會不會影響到性能呢?有多
            大的影響呢?
                測試代碼如下:
                    #include <iostream>
                    #include <ctime>
                    #include <vector>
                    #include <list>
                    #include <map>
                    #include <algorithm>
                    
                    #include <boost/lambda/lambda.hpp>
                    #include <boost/lambda/bind.hpp>
                   
                    #define MAX_TEST_COUNT 1000 * 1000 * 2
                    #define TIME_ELAPSE() ( std::clock() - start * 1.0 ) / CLOCKS_PER_SEC
                   
                    int main( int argc, char *argv[] )
                    {
                        double times[6];
                        size_t idx = 0;
                        /*
                         * vector
                         */
                        std::vector<size_t> vec1;
                        std::clock_t start = std::clock();
                        {
                            for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
                            {
                                vec1.push_back( i );
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                   
                        std::vector<std::string> vec2;
                        start = std::clock();
                        {
                            for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
                            {
                                vec2.push_back( "std::vector<std::string>" );
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                        /*
                         * list
                         */
                        std::list<size_t> lst1;
                        start = std::clock();
                        {
                            for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
                            {
                                lst1.push_back( i );
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                   
                        std::list<std::string> lst2;
                        start = std::clock();
                        {
                            for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
                            {
                                lst2.push_back( "std::vector<std::string>" );
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                        /*
                         * map
                         */
                        std::map<size_t, size_t> map1;
                        start = std::clock();
                        {
                            for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
                            {
                                map1.insert( std::make_pair( i, i ) );
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                   
                        std::map<size_t, std::string> map2;
                        start = std::clock();
                        {
                            for ( size_t i = 0; i < MAX_TEST_COUNT; ++i )
                            {
                                map2.insert( std::make_pair( i, "std::vector<std::string>" ) );
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                   
                        std::cout << "std::vector<size_t>::push_back        " << times[0] << " s" << std::endl;
                        std::cout << "std::vector<std::string>::push_back   " << times[1] << " s" << std::endl;
                        std::cout << "std::list<size_t>::push_back          " << times[2] << " s" << std::endl;
                        std::cout << "std::list<std::string>::push_back     " << times[3] << " s" << std::endl;
                        std::cout << "std::map<size_t, size_t>::insert      " << times[4] << " s" << std::endl;
                        std::cout << "std::map<size_t, std::string>::insert " << times[5] << " s" << std::endl;
                   
                        std::cout << std::endl;
                        /*
                         * iterator
                         */
                        idx = 0;
                        start = std::clock();
                        {
                            for ( std::vector<size_t>::iterator it = vec1.begin(); it != vec1.end(); ++it )
                            {
                                (*it) = (*it) + 1;
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                        start = std::clock();
                        {
                            for ( std::vector<std::string>::iterator it = vec2.begin(); it != vec2.end(); ++it )
                            {
                                (*it) = "std::vector<std::string>::iterator";
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                        start = std::clock();
                        {
                            for ( std::list<size_t>::iterator it = lst1.begin(); it != lst1.end(); ++it )
                            {
                                (*it) = (*it) + 1;
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                        start = std::clock();
                        {
                            for ( std::list<std::string>::iterator it = lst2.begin(); it != lst2.end(); ++it )
                            {
                                (*it) = "std::list<std::string>::iterator";
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                        start = std::clock();
                        {
                            for ( std::map<size_t, size_t>::iterator it = map1.begin(); it != map1.end(); ++it )
                            {
                                (*it).second = (*it).second + 1;
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                        start = std::clock();
                        {
                            for ( std::map<size_t, std::string>::iterator it = map2.begin(); it != map2.end(); ++it )
                            {
                                (*it).second = "std::map<size_t, std::string>::iterator";
                            }
                        }
                        times[idx++] = TIME_ELAPSE();
                   
                        std::cout << "std::vector<size_t>::iterator           " << times[0] << " s" << std::endl;
                        std::cout << "std::vector<std::string>::iterator      " << times[1] << " s" << std::endl;
                        std::cout << "std::list<size_t>::iterator             " << times[2] << " s" << std::endl;
                        std::cout << "std::list<std::string>::iterator        " << times[3] << " s" << std::endl;
                        std::cout << "std::map<size_t, size_t>::iterator      " << times[4] << " s" << std::endl;
                        std::cout << "std::map<size_t, std::string>::iterator " << times[5] << " s" << std::endl;
                   
                        std::cout << std::endl;
                        /*
                         * for_each
                         */
                        using namespace boost::lambda;
                        idx = 0;
                   
                        start = std::clock();
                        {
                            std::for_each( vec1.begin(), vec1.end(), _1 = _1 + 1 );
                        }
                        times[idx++] = TIME_ELAPSE();
                        start = std::clock();
                        {
                            std::for_each( vec2.begin(), vec2.end(), _1 = "std::for_each" );
                        }
                        times[idx++] = TIME_ELAPSE();
                        start = std::clock();
                        {
                            std::for_each( lst1.begin(), lst1.end(), _1 = _1 + 1 );
                        }
                        times[idx++] = TIME_ELAPSE();
                        start = std::clock();
                        {
                            std::for_each( lst2.begin(), lst2.end(), _1 = "std::for_each" );
                        }
                        times[idx++] = TIME_ELAPSE();
                        start = std::clock();
                        {
                            std::for_each( map1.begin(), map1.end(), bind( &std::map<size_t, size_t>::value_type::second, _1 ) = bind( &std::map<size_t, size_t>::value_type::second, _1 ) + 1 );
                        }
                        times[idx++] = TIME_ELAPSE();
                        start = std::clock();
                        {
                            std::for_each( map2.begin(), map2.end(), bind( &std::map<size_t, std::string>::value_type::second, _1 ) = "std::for_each" );
                        }
                        times[idx++] = TIME_ELAPSE();
                   
                        std::cout << "std::vector<size_t>::for_each           " << times[0] << " s" << std::endl;
                        std::cout << "std::vector<std::string>::for_each      " << times[1] << " s" << std::endl;
                        std::cout << "std::list<size_t>::for_each             " << times[2] << " s" << std::endl;
                        std::cout << "std::list<std::string>::for_each        " << times[3] << " s" << std::endl;
                        std::cout << "std::map<size_t, size_t>::for_each      " << times[4] << " s" << std::endl;
                        std::cout << "std::map<size_t, std::string>::for_each " << times[5] << " s" << std::endl;
                   
                        return 0;
                    }
                我的測試環境如下:
                    軟件環境:Visual Studio2005 Pro + SP1, boost1.34.0
                    硬件環境:PentiumD 3.0GHz, 4G RAM
                測試數據:
                    1. 默認開啟safe code
                       std::vector<size_t>::push_back        0.031 s
                       std::vector<std::string>::push_back   2.609 s
                       std::list<size_t>::push_back          0.797 s
                       std::list<std::string>::push_back     2.813 s
                       std::map<size_t, size_t>::insert      1.906 s
                       std::map<size_t, std::string>::insert 4.047 s

                       std::vector<size_t>::iterator           0 s
                       std::vector<std::string>::iterator      1.718 s
                       std::list<size_t>::iterator             0.016 s
                       std::list<std::string>::iterator        1.734 s
                       std::map<size_t, size_t>::iterator      0.063 s
                       std::map<size_t, std::string>::iterator 1.891 s

                       std::vector<size_t>::for_each           0.015 s
                       std::vector<std::string>::for_each      0.156 s
                       std::list<size_t>::for_each             0.016 s
                       std::list<std::string>::for_each        0.156 s
                       std::map<size_t, size_t>::for_each      0.078 s
                       std::map<size_t, std::string>::for_each 0.313 s

                    2. 關閉safe code(通過定義如下宏:_CRT_SECURE_NO_DEPRECATE,
                                                      _SCL_SECURE_NO_DEPRECATE,
                                                      _SECURE_SCL=0)
                       std::vector<size_t>::push_back        0.031 s
                       std::vector<std::string>::push_back   2.594 s
                       std::list<size_t>::push_back          0.796 s
                       std::list<std::string>::push_back     2.813 s
                       std::map<size_t, size_t>::insert      1.875 s
                       std::map<size_t, std::string>::insert 4.047 s

                       std::vector<size_t>::iterator           0 s
                       std::vector<std::string>::iterator      1.703 s
                       std::list<size_t>::iterator             0.031 s
                       std::list<std::string>::iterator        1.719 s
                       std::map<size_t, size_t>::iterator      0.062 s
                       std::map<size_t, std::string>::iterator 1.891 s

                       std::vector<size_t>::for_each           0.016 s
                       std::vector<std::string>::for_each      0.14 s
                       std::list<size_t>::for_each             0.016 s
                       std::list<std::string>::for_each        0.172 s
                       std::map<size_t, size_t>::for_each      0.062 s
                       std::map<size_t, std::string>::for_each 0.313 s

                由上面的測試數據可以的出如下的結論:
                1. safe code對代碼的性能確實是有影響的,畢竟加入了判斷的代碼;
                2. safe code對代碼的性能影響非常的小(最大沒有超過5%),基本上為了安全型可以
                   忽略其影響,如果確實比較注重性能,可以考慮在release版本下關閉safe code檢查,
                   在debug版下還是打開以發現錯誤比較好。
             
            注:宏解釋
                _CRT_SECURE_NO_DEPRECATE和_SCL_SECURE_NO_DEPRECATE用于關閉safe code代碼警告;
                _SECURE_SCL用于控制是否采用safe code對STL邊界進行檢查。

            Feedback

            # re: MSVC8中的SafeCode對性能的影響  回復  更多評論   

            2007-06-11 12:50 by pass86
            。NET 2003,應該就沒有。

            # re: MSVC8中的SafeCode對性能的影響  回復  更多評論   

            2007-06-11 13:18 by chemz
            是的,safe code是在Visual C++2005以上版本添加上去的。visual C++.NET2003中的STL庫已經非常的不錯了,標準兼容性也很好。我最開始使用2003的時候使用了STLport4.6.2替換了2003中的STL實現,感覺還不錯的

            # re: MSVC8中的SafeCode對性能的影響  回復  更多評論   

            2007-06-11 14:47 by 空明流轉
            50%的性能差異是在隨即容器隨機讀寫下完成的測試。某牛說。

            # re: MSVC8中的SafeCode對性能的影響  回復  更多評論   

            2007-10-11 10:06 by 金慶
            push_back()是不需要邊界檢查的,所以性能測試無效:
            vec1.push_back( i );
            色99久久久久高潮综合影院| 久久综合亚洲欧美成人| 91久久精一区二区三区大全| 久久久久久综合一区中文字幕| 一级做a爰片久久毛片人呢| 国产毛片久久久久久国产毛片| 久久久久18| 精品久久久久久无码专区| 久久婷婷国产麻豆91天堂| 亚洲AV伊人久久青青草原| 国产91久久精品一区二区| 亚洲国产成人久久综合野外| 久久精品亚洲中文字幕无码麻豆| 99久久99久久精品国产片| 久久青青色综合| 夜夜亚洲天天久久| 无码人妻久久久一区二区三区| 久久成人影院精品777| 国产精品久久久久免费a∨| 日本道色综合久久影院| 性做久久久久久久| 午夜精品久久久久久影视777| 99国产欧美久久久精品蜜芽| 亚洲精品tv久久久久久久久久| 色噜噜狠狠先锋影音久久| 久久夜色精品国产噜噜噜亚洲AV| 久久夜色精品国产www| 久久久久久久综合日本亚洲| 亚洲综合日韩久久成人AV| 中文字幕无码av激情不卡久久| 精品国产乱码久久久久久浪潮| 99久久精品国内| 久久99国产综合精品女同| 亚洲精品蜜桃久久久久久| 色天使久久综合网天天| 亚洲欧美精品一区久久中文字幕 | 潮喷大喷水系列无码久久精品| 久久久精品久久久久影院| 伊人久久无码精品中文字幕| 久久久久久极精品久久久| 理论片午午伦夜理片久久|