久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <small id='uDp5n'></small><noframes id='uDp5n'>

    <i id='uDp5n'><tr id='uDp5n'><dt id='uDp5n'><q id='uDp5n'><span id='uDp5n'><b id='uDp5n'><form id='uDp5n'><ins id='uDp5n'></ins><ul id='uDp5n'></ul><sub id='uDp5n'></sub></form><legend id='uDp5n'></legend><bdo id='uDp5n'><pre id='uDp5n'><center id='uDp5n'></center></pre></bdo></b><th id='uDp5n'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='uDp5n'><tfoot id='uDp5n'></tfoot><dl id='uDp5n'><fieldset id='uDp5n'></fieldset></dl></div>
    <tfoot id='uDp5n'></tfoot>

      <bdo id='uDp5n'></bdo><ul id='uDp5n'></ul>

        <legend id='uDp5n'><style id='uDp5n'><dir id='uDp5n'><q id='uDp5n'></q></dir></style></legend>
      1. 在 C++11 中計(jì)算編譯時(shí)(constexpr)中的斐波那契數(shù)(遞

        Calculate the Fibonacci number (recursive approach) in compile time (constexpr) in C++11(在 C++11 中計(jì)算編譯時(shí)(constexpr)中的斐波那契數(shù)(遞歸方法))

          <small id='FFJ90'></small><noframes id='FFJ90'>

            <bdo id='FFJ90'></bdo><ul id='FFJ90'></ul>
            <legend id='FFJ90'><style id='FFJ90'><dir id='FFJ90'><q id='FFJ90'></q></dir></style></legend>
              <tbody id='FFJ90'></tbody>

              • <tfoot id='FFJ90'></tfoot>
                1. <i id='FFJ90'><tr id='FFJ90'><dt id='FFJ90'><q id='FFJ90'><span id='FFJ90'><b id='FFJ90'><form id='FFJ90'><ins id='FFJ90'></ins><ul id='FFJ90'></ul><sub id='FFJ90'></sub></form><legend id='FFJ90'></legend><bdo id='FFJ90'><pre id='FFJ90'><center id='FFJ90'></center></pre></bdo></b><th id='FFJ90'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='FFJ90'><tfoot id='FFJ90'></tfoot><dl id='FFJ90'><fieldset id='FFJ90'></fieldset></dl></div>

                  本文介紹了在 C++11 中計(jì)算編譯時(shí)(constexpr)中的斐波那契數(shù)(遞歸方法)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我在編譯時(shí)編寫了斐波那契數(shù)計(jì)算程序(constexpr)使用 C++11 支持的模板元編程技術(shù)的問題.目的這是為了計(jì)算模板元編程方法與舊的傳統(tǒng)方法之間的運(yùn)行時(shí)間差異.

                  I wrote the program Fibonacci number calculation in compile time (constexpr) problem using the template metaprogramming techniques supported in C++11. The purpose of this is to calculate the difference in the run-time between the template metaprogramming approach and the old conventional approach.

                  // Template Metaprograming Approach
                  template<int  N>
                  constexpr int fibonacci() {return fibonacci<N-1>() + fibonacci<N-2>(); }
                  template<>
                  constexpr int fibonacci<1>() { return 1; }
                  template<>
                  constexpr int fibonacci<0>() { return 0; }
                  
                  
                  
                  // Conventional Approach
                   int fibonacci(int N) {
                     if ( N == 0 ) return 0;
                     else if ( N == 1 ) return 1;
                     else
                        return (fibonacci(N-1) + fibonacci(N-2));
                  } 
                  

                  我在我的 GNU/Linux 系統(tǒng)上為 N = 40 運(yùn)行了兩個(gè)程序,并測(cè)量了時(shí)間和發(fā)現(xiàn)傳統(tǒng)解決方案(1.15 秒)比基于模板的解決方案(0.55 秒)慢大約兩倍.這是一項(xiàng)重大改進(jìn),因?yàn)檫@兩種方法都基于遞歸.

                  I ran both programs for N = 40 on my GNU/Linux system and measured the time and found that that conventional solution (1.15 second) is around two times slower than the template-based solution (0.55 second). This is a significant improvement as both approaches are based on the recursion.

                  為了更深入地理解它,我在 g++ 中編譯了程序(-fdump-tree-all 標(biāo)志),發(fā)現(xiàn)編譯器實(shí)際上生成了 40 個(gè)不同的函數(shù)(如 fibonacci<40>、fibonacci<39>...斐波那契<0>).

                  To understand it more I compiled the program (-fdump-tree-all flag) in g++ and found that compiler actually generated the 40 different functions (like fibonacci<40>, fibonacci<39>...fibonacci<0>).

                  constexpr int fibonacci() [with int N = 40] () {
                    int D.29948, D.29949, D.29950;
                    D.29949 = fibonacci<39> ();
                    D.29950 = fibonacci<38> ();
                    D.29948 = D.29949 + D.29950;
                    return D.29948;
                  }
                  
                  constexpr int fibonacci() [with int N = 39] () {
                    int D.29952, D.29953, D.29954;
                    D.29953 = fibonacci<38> ();
                    D.29954 = fibonacci<37> ();
                    D.29952 = D.29953 + D.29954;
                    return D.29952;
                  }
                  ...
                  ...
                  ...
                  constexpr int fibonacci() [with int N = 0] () {
                    int D.29962;
                    D.29962 = 0;
                    return D.29962;
                  }
                  

                  我也在GDB中調(diào)試了程序,發(fā)現(xiàn)上面的功能都是執(zhí)行次數(shù)與傳統(tǒng)遞歸方法相同.如果程序的兩個(gè)版本都以相同的次數(shù)(遞歸)執(zhí)行該函數(shù),那么模板元編程技術(shù)如何實(shí)現(xiàn)這一點(diǎn)?我還想知道您對(duì)基于模板元編程的方法與其他版本相比如何以及為什么要花費(fèi)一半時(shí)間的看法?這個(gè)程序能不能比現(xiàn)在的程序更快?

                  I also debugged the program in GDB and found that all the above functions are executed an equal number of times as with the conventional recursive approach. If both versions of the program are executing the function an equal number of times (recursive), then how is this achieved by template metaprogramming techniques? I would also like to know your opinion about how and why a template metaprogramming based approach is taking half time compared to the other version? Can this program be made faster than the current one?

                  基本上,我的目的是盡可能多地了解內(nèi)部發(fā)生的事情.

                  Basically my intention here is to understand what's going on internally as much as possible.

                  我的機(jī)器是帶有 GCC 4.8.1 的 GNU/Linux,我對(duì)兩個(gè)程序都使用了優(yōu)化 -o3.

                  My machine is GNU/Linux with GCC 4.8.1, and I used the optimization -o3 for both programs.

                  推薦答案

                  試試這個(gè):

                  template<size_t N>
                  struct fibonacci : integral_constant<size_t, fibonacci<N-1>{} + fibonacci<N-2>{}> {};
                  
                  template<> struct fibonacci<1> : integral_constant<size_t,1> {};
                  template<> struct fibonacci<0> : integral_constant<size_t,0> {};
                  

                  使用 clang 和 -Os,編譯時(shí)間大約為 0.5 秒, 時(shí)間運(yùn)行 N=40.您的傳統(tǒng)"方法大約在 0.4 秒內(nèi)編譯并在 0.8 秒內(nèi)運(yùn)行.只是為了檢查,結(jié)果是102334155對(duì)嗎?

                  With clang and -Os, this compiles in roughly 0.5s and runs in zero time for N=40. Your "conventional" approach compiles in roughly 0.4s and runs in 0.8s. Just for checking, the result is 102334155 right?

                  當(dāng)我嘗試您自己的 constexpr 解決方案時(shí),編譯器運(yùn)行了幾分鐘,然后我停止了它,因?yàn)轱@然內(nèi)存已滿(計(jì)算機(jī)開始凍結(jié)).編譯器正在嘗試計(jì)算最終結(jié)果,而您的實(shí)現(xiàn)在編譯時(shí)使用效率極低.

                  When I tried your own constexpr solution the compiler run for a couple of minutes and then I stopped it because apparently memory was full (computer started freezing). The compiler was trying to compute the final result and your implementation is extremely inefficient to be used at compile time.

                  使用此解決方案,在實(shí)例化 N 時(shí)會(huì)重復(fù)使用 N-2N-1 處的模板實(shí)例化.所以 fibonacci<40> 實(shí)際上在編譯時(shí)作為一個(gè)值被知道,在運(yùn)行時(shí)沒有什么可做的.這是一種動(dòng)態(tài)編程方法,當(dāng)然,如果您在 N<計(jì)算之前將所有值存儲(chǔ)在 0N-1 處,當(dāng)然您可以在運(yùn)行時(shí)執(zhí)行相同的操作/代碼>.

                  With this solution, template instantiations at N-2, N-1 are re-used when instantiating N. So fibonacci<40> is actually known at compile time as a value, and there is nothing to do at run-time. This is a dynamic programming approach and of course you can do the same at run time if you store all values at 0 through N-1 before computing at N.

                  使用您的解決方案,編譯器可以在編譯時(shí)評(píng)估fibonacci(),但不需要.在您的情況下,所有或部分計(jì)算都留給運(yùn)行時(shí).就我而言,所有計(jì)算都是在編譯時(shí)嘗試的,因此永無止境.

                  With your solution, the compiler can evaluate fibonacci<N>() at compile time but is not required to. In your case, all or part of computation is left for run time. In my case, all computation is attempted at compile time, hence never ending.

                  這篇關(guān)于在 C++11 中計(jì)算編譯時(shí)(constexpr)中的斐波那契數(shù)(遞歸方法)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  In what ways do C++ exceptions slow down code when there are no exceptions thown?(當(dāng)沒有異常時(shí),C++ 異常會(huì)以何種方式減慢代碼速度?)
                  Why catch an exception as reference-to-const?(為什么要捕獲異常作為對(duì) const 的引用?)
                  When and how should I use exception handling?(我應(yīng)該何時(shí)以及如何使用異常處理?)
                  Scope of exception object in C++(C++中異常對(duì)象的范圍)
                  Catching exceptions from a constructor#39;s initializer list(從構(gòu)造函數(shù)的初始化列表中捕獲異常)
                  Difference between C++03 throw() specifier C++11 noexcept(C++03 throw() 說明符 C++11 noexcept 之間的區(qū)別)

                  <small id='pkhVu'></small><noframes id='pkhVu'>

                  • <legend id='pkhVu'><style id='pkhVu'><dir id='pkhVu'><q id='pkhVu'></q></dir></style></legend>
                          <bdo id='pkhVu'></bdo><ul id='pkhVu'></ul>
                          • <i id='pkhVu'><tr id='pkhVu'><dt id='pkhVu'><q id='pkhVu'><span id='pkhVu'><b id='pkhVu'><form id='pkhVu'><ins id='pkhVu'></ins><ul id='pkhVu'></ul><sub id='pkhVu'></sub></form><legend id='pkhVu'></legend><bdo id='pkhVu'><pre id='pkhVu'><center id='pkhVu'></center></pre></bdo></b><th id='pkhVu'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='pkhVu'><tfoot id='pkhVu'></tfoot><dl id='pkhVu'><fieldset id='pkhVu'></fieldset></dl></div>
                              <tbody id='pkhVu'></tbody>

                            <tfoot id='pkhVu'></tfoot>
                            主站蜘蛛池模板: 国产日韩精品一区二区 | 国产精品国产三级国产aⅴ浪潮 | 999久久久国产精品 亚洲黄色三级 | 色婷婷久久 | 天天干b | 福利视频1000 | 青青草在线观看视频 | 亚洲精品一区二区三 | 久热久草| 草草免费视频 | a天堂在线观看 | av女人天堂| 久久久在线视频 | 欧美日韩国产中文 | 91免费版看片 | 午夜看看| 性欧美bbw| 成人免费视频视频 | 成人爽a毛片一区二区免费 亚洲午夜在线观看 | 中文字幕伊人 | 国产精品久久久久久久久久久久午夜片 | 亚洲 欧美 日韩 在线 | 黄色av网站在线观看 | 日韩欧美一级 | 理论片中文字幕 | www.中文字幕| 91精品视频在线播放 | 成人午夜在线视频 | 99精品网| 精品少妇一区二区三区免费观 | 91精品一区 | 色片在线 | 亚洲第一综合 | 精品在线一区 | 色综合久久天天综合网 | 亚洲激情一区二区 | 黄色大片在线免费观看 | 久久久国产精品人人片 | 久久精品视频网 | 久久香蕉国产 | 久久99精品久久久久久琪琪 |