久久久久久久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 中計算編譯時(constexpr)中的斐波那契數(遞

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

          <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 中計算編譯時(constexpr)中的斐波那契數(遞歸方法)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

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

                  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 系統上為 N = 40 運行了兩個程序,并測量了時間和發現傳統解決方案(1.15 秒)比基于模板的解決方案(0.55 秒)慢大約兩倍.這是一項重大改進,因為這兩種方法都基于遞歸.

                  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 標志),發現編譯器實際上生成了 40 個不同的函數(如 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中調試了程序,發現上面的功能都是執行次數與傳統遞歸方法相同.如果程序的兩個版本都以相同的次數(遞歸)執行該函數,那么模板元編程技術如何實現這一點?我還想知道您對基于模板元編程的方法與其他版本相比如何以及為什么要花費一半時間的看法?這個程序能不能比現在的程序更快?

                  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?

                  基本上,我的目的是盡可能多地了解內部發生的事情.

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

                  我的機器是帶有 GCC 4.8.1 的 GNU/Linux,我對兩個程序都使用了優化 -o3.

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

                  推薦答案

                  試試這個:

                  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,編譯時間大約為 0.5 秒, 時間運行 N=40.您的傳統"方法大約在 0.4 秒內編譯并在 0.8 秒內運行.只是為了檢查,結果是102334155對嗎?

                  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?

                  當我嘗試您自己的 constexpr 解決方案時,編譯器運行了幾分鐘,然后我停止了它,因為顯然內存已滿(計算機開始凍結).編譯器正在嘗試計算最終結果,而您的實現在編譯時使用效率極低.

                  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.

                  使用此解決方案,在實例化 N 時會重復使用 N-2N-1 處的模板實例化.所以 fibonacci<40> 實際上在編譯時作為一個值被知道,在運行時沒有什么可做的.這是一種動態編程方法,當然,如果您在 N<計算之前將所有值存儲在 0N-1 處,當然您可以在運行時執行相同的操作/代碼>.

                  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.

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

                  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.

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

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

                  相關文檔推薦

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

                  <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>
                            主站蜘蛛池模板: 精品久久香蕉国产线看观看亚洲 | 神马久久久久久久久久 | 国产2区 | 四虎成人av | 亚洲国产69 | 亚洲一级av毛片 | 亚洲精品久久久久久久久久久久久 | 国产激情小视频 | 国产成人精品网站 | 日韩福利视频 | 亚洲成人动漫在线观看 | www成年人视频 | 色婷婷一区二区三区四区 | 91国内视频在线 | 爱草在线 | 精品1区2区 | 福利社午夜影院 | 色视频免费| 成人国产精品久久久 | 日本人爽p大片免费看 | 欧美日韩不卡在线 | 中文字幕在线一区 | 国产在线一级片 | 久久久91精品国产一区二区三区 | 久久精品亚洲 | 91精品国产欧美一区二区成人 | 亚洲精品在线视频 | 久久精品福利 | www.久久精品视频 | 欧美在线色 | 久久久久久久久一区 | 日韩网站在线 | 亚洲免费在线观看 | 一级免费在线视频 | 在线视频一区二区三区 | 激情欧美一区二区三区中文字幕 | 人人干人人干人人 | 久久久高清 | 国产精品一区二区三区在线 | com.国产| 日韩福利 |