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

        <tfoot id='PfKVT'></tfoot>

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

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

        哪個更快?按引用傳遞與按值傳遞 C++

        Which is faster? Pass by reference vs pass by value C++(哪個更快?按引用傳遞與按值傳遞 C++)
          1. <i id='1icFd'><tr id='1icFd'><dt id='1icFd'><q id='1icFd'><span id='1icFd'><b id='1icFd'><form id='1icFd'><ins id='1icFd'></ins><ul id='1icFd'></ul><sub id='1icFd'></sub></form><legend id='1icFd'></legend><bdo id='1icFd'><pre id='1icFd'><center id='1icFd'></center></pre></bdo></b><th id='1icFd'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='1icFd'><tfoot id='1icFd'></tfoot><dl id='1icFd'><fieldset id='1icFd'></fieldset></dl></div>

                <tbody id='1icFd'></tbody>

            • <legend id='1icFd'><style id='1icFd'><dir id='1icFd'><q id='1icFd'></q></dir></style></legend>
            • <small id='1icFd'></small><noframes id='1icFd'>

                <tfoot id='1icFd'></tfoot>

                  <bdo id='1icFd'></bdo><ul id='1icFd'></ul>
                  本文介紹了哪個更快?按引用傳遞與按值傳遞 C++的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我認為通過引用傳遞應該比通過值傳遞更快,因為計算機不會復制數據,它只是指向數據的地址.

                  I thought that pass by reference should be faster then pass by value because the computer isn't copying data, it just points to the address of data.

                  但是,請考慮以下 C++ 代碼:

                  But, consider the following C++ code:

                  #include <iostream>
                  #include <cassert>
                  #include <cmath>
                  
                  using namespace std;
                  
                  // do not use pass by reference& with this function, it will become so slow
                  unsigned long long computePascal(const unsigned int row, const unsigned int position) {
                      if (position == 1 || position == row)
                      return 1L;
                      unsigned long long left = computePascal(row-1, position-1);
                      unsigned long long right = computePascal(row-1, position);
                      unsigned long long sum = left + right;
                      return sum;
                  }
                  
                  int main() {
                      int row, position;
                      cout << "Input row and position: ";
                      cin >> row >> position;
                      assert(position > 0 && position <= row);
                      unsigned long long pascalNumber = computePascal(row, position);
                      cout << pascalNumber << endl;
                      return 0;
                  }
                  

                  現在這段代碼只是一個普通的程序,它通過輸入三角形的所需行和位置來遞歸計算帕斯卡數.

                  Now this code is just a normal program that compute pascal number recursively by entering the desired row and position of the triangle.

                  我嘗試放置第 50 行和第 7 行,它計算大約 1 秒(按值傳遞).輸出大約是 1300 萬,這是正確的.所以我認為如果我通過引用傳遞它可能會更快,因為它不需要復制大量數據.但這是非常錯誤的,我不知道為什么需要更長的時間,大約是傳遞值所需的時間的 3 倍.問題是……

                  I tried putting row 50 and position 7 and it computes around 1 second (pass by value). The output is about 13 million which is correct. So I thought that it could be faster if I pass by reference instead because it doesn't need to copy a lot of data. But this is very wrong and I don't know why it took longer time about 3 times the amount it took for passing by value. The question is ...

                  為什么這個程序在我嘗試將其更改為通過常量引用后計算如此緩慢?

                  因為遞歸函數是一個例外,你應該通過值而不是常量引用傳遞,這有關系嗎?

                  Does it matter that because recursive function is an exception that you should pass by value rather than const reference?

                  推薦答案

                  哪個更快"的答案通常是這取決于".

                  The answer to "Which is faster" is usually "It depends".

                  如果不是傳遞 4 個字節的數據,而是傳遞一個指向數據的 8 個字節的指針,那么您真的不能指望這會讓事情變得更快.如果不是傳遞 100 個字節的數據,而是傳遞一個指向數據的 8 個字節的指針,那就不同了.

                  If instead of passing four bytes of data you are passing an eight byte pointer to data, then you can't really expect that to make things faster. If instead of passing 100 bytes of data you are passing an eight byte pointer to data, that's different.

                  但是現在函數沒有數據,它只有一個引用.所以每當它需要讀取數據時,它必須通過引用間接地做到這一點.那需要更長的時間.如果您傳遞一個 100 字節的對象并且只讀取其中的 8 個字節,您仍然有可能獲勝.但是,如果您實際上讀取了所有數據,并且可能多次讀取,那么即使對于大對象,傳遞值也很容易更快.

                  But now the function doesn't have the data, it only has a reference. So whenever it needs to read the data, it has to do that indirectly through the reference. That takes longer. If you pass a 100 byte object and only read eight byte of it, you still are likely to win. But if you actually read all the data, and maybe multiple times, then it could easily be faster to pass the value even for large objects.

                  真正的區別在于您傳遞對象時,而按值傳遞意味著將調用或多或少復雜的構造函數.通過引用傳遞意味著沒有構造函數.但是 int 無論如何都沒有構造函數.

                  The real difference comes when you pass an object, and passing by value means a more or less complex constructor will be called. Passing by reference means no constructor. But int has no constructor anyway.

                  然后是優化.按值傳遞意味著編譯器知道您的函數是唯一可以訪問數據的函數.通過引用傳遞意味著數據可以在任何地方.如果你有兩個 int&參數,我可以傳遞一些 int 兩次.所以增加行可能增加位置.或者它可能不會.這會扼殺優化.

                  And then there is optimisation. Passing by value means the compiler knows your function is the only one with access to the data. Pass by reference means the data could be anywhere. If you have two int& parameters, I could pass the some int twice. So increasing row might increase pos. Or it might not. That kills optimisations.

                  然后是優化規則:衡量它".你測量了它,發現什么更快.有時事情會無緣無故地更快或更慢.

                  And then there is the rule of optimisation: "Measure it". You measured it and found what's faster. Sometimes things are faster or slower for no good reason whatsoever.

                  這篇關于哪個更快?按引用傳遞與按值傳遞 C++的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 之間的區別)
                  <i id='GaHaa'><tr id='GaHaa'><dt id='GaHaa'><q id='GaHaa'><span id='GaHaa'><b id='GaHaa'><form id='GaHaa'><ins id='GaHaa'></ins><ul id='GaHaa'></ul><sub id='GaHaa'></sub></form><legend id='GaHaa'></legend><bdo id='GaHaa'><pre id='GaHaa'><center id='GaHaa'></center></pre></bdo></b><th id='GaHaa'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='GaHaa'><tfoot id='GaHaa'></tfoot><dl id='GaHaa'><fieldset id='GaHaa'></fieldset></dl></div>
                    <legend id='GaHaa'><style id='GaHaa'><dir id='GaHaa'><q id='GaHaa'></q></dir></style></legend>
                        <bdo id='GaHaa'></bdo><ul id='GaHaa'></ul>
                          <tbody id='GaHaa'></tbody>

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

                          <tfoot id='GaHaa'></tfoot>

                            主站蜘蛛池模板: 夜夜艹天天干 | 99综合| 在线成人免费视频 | 成人黄色电影在线播放 | 久久伊人精品一区二区三区 | 日本午夜免费福利视频 | 欧美1页 | 日本a级大片 | 亚洲在线免费观看 | 午夜精品久久久久久久久久久久久 | 亚洲视频在线一区 | 成人做爰69片免费观看 | 91精品福利 | 久久精品亚洲国产奇米99 | 亚洲一区视频在线 | 日日夜夜91 | 伊人二区| 欧美综合一区二区三区 | 久久69精品久久久久久久电影好 | 日韩视频在线一区 | 亚洲视频在线看 | 91麻豆精品国产91久久久更新资源速度超快 | 欧美亚洲国产日韩 | 国产日产久久高清欧美一区 | 一区二区在线观看av | av网站免费观看 | 一区二区三区影院 | 久久精品二区亚洲w码 | 久久美女网 | 久久久精品网 | 欧美福利 | 波多野结衣在线观看一区二区三区 | 日韩第一夜 | 老司机久久 | 精品国产色 | www.久| 91爱啪啪 | 亚洲综合国产精品 | 国产精品久久久久国产a级 欧美日韩国产免费 | 日本一区二区三区四区 | av午夜电影 |