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

性能權(quán)衡 - MATLAB 何時比 C/C++ 更好/更慢

Performance Tradeoff - When is MATLAB better/slower than C/C++(性能權(quán)衡 - MATLAB 何時比 C/C++ 更好/更慢)
本文介紹了性能權(quán)衡 - MATLAB 何時比 C/C++ 更好/更慢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我知道 C/C++ 是一種低級語言,當我們與任何其他高級語言進行比較時,它會生成相對優(yōu)化的機器代碼.但我猜想的遠不止這些,從實踐中也可以看出這一點.

I am aware that C/C++ is a lower-level language and generates relatively optimized machine code when we compare with any other high-level language. But I guess there is pretty much more than that, which is also evident from the practice.

當我對高斯樣本集合進行蒙特卡洛平均等簡單計算時,我發(fā)現(xiàn) C++ 實現(xiàn)或 MATLAB 實現(xiàn)之間沒有太大區(qū)別,有時實際上 MATLAB 的性能在時間上要好一些.

When I do simple calculations like montecarlo averaging of a Gaussian sample collection or so, I see there is not much of a difference between a C++ implementation or MATLAB implementation, sometimes in fact MATLAB performs a bit better in time.

當我繼續(xù)使用數(shù)千行代碼進行更大規(guī)模的模擬時,慢慢地就會出現(xiàn)真實的畫面.C++ 仿真顯示出卓越的性能,在時間復(fù)雜度上比等效的 MATLAB 實現(xiàn)高 100 倍.

When I move on to larger scale simulations with thousands of lines of code, slowly the real picture shows up. C++ simulations show superior performance like 100x better in time complexity than an equivalent MATLAB implementation.

大多數(shù)情況下,C++ 中的代碼幾乎是連續(xù)的,并且沒有明確進行高保真優(yōu)化.然而,據(jù)我所知,MATLAB 本質(zhì)上做了很多優(yōu)化.例如,當我嘗試生成大量隨機樣本時,就會出現(xiàn)這種情況,而在 C++ 中,使用 IT++/GSL/Boost 等庫的等效項的執(zhí)行速度相對較慢(使用的算法相同,即 mt19937).

The code in C++ most of the times, is pretty much serial and no hi-fi optimization is done explicitly. Whereas, as per my awareness, MATLAB inherently does a lot of optimization. This shows up for example when I try to generate a huge chunk of random samples, where as the equivalent in C++ using some library like IT++/GSL/Boost performs relatively slower (the algorithm used is the same namely mt19937).

我的問題只是想知道 MATLAB/C++ 在性能方面是否有更簡單的權(quán)衡.是不是就像人們所說的無論何時,C/C++ 都是更好的"(經(jīng)常經(jīng)歷的)?.換個角度看,除了舒適,MATLAB 還有什么好處?"

順便說一句,我認為這里的編碼效率參數(shù)并不重要,在這兩種情況下考慮的是同一個程序員.而且,我認為其他替代品如 python,R 在這里不相關(guān).但是對我們使用的特定庫的依賴應(yīng)該很有趣.

By the way, I don't see coding efficiency parameter being significant here, thinking of the same programmer in both cases. And also, I think the other alternatives like python,R are not relevant here. But dependence on the specific libraries we use should be interesting.

[我是通信系統(tǒng)編碼理論的博士生.我一直在使用 matlab/C++ 進行模擬,并且在兩種情況下都編寫了幾萬行代碼的合理經(jīng)驗]

推薦答案

我已經(jīng)使用 Matlab 和 C++ 大約 10 年了.對于為我的研究實施的每個數(shù)值算法,我總是從使用 Matlab 進行原型設(shè)計開始,然后將項目轉(zhuǎn)換為 C++ 以獲得 10 到 100 倍(我不是在開玩笑)的性能改進.當然,我將優(yōu)化的 C++ 代碼與完全矢量化的 Matlab 代碼進行比較.平均而言,改進幅度約為 50 倍.

I have been using Matlab and C++ for about 10 years. For every numerical algorithms implemented for my research, I always start from prototyping with Matlab and then translate the project to C++ to gain a 10x to 100x (I am not kidding) performance improvement. Of course, I am comparing optimized C++ code to the fully vectorized Matlab code. On average, the improvement is about 50x.

這兩種編程語言背后都有很多微妙之處,以下是一些誤解:

There are lot of subtleties behind both of the two programming languages, and the following are some misunderstandings:

  1. Matlab 是一種腳本語言,但 C++ 是編譯的

Matlab 使用 JIT 編譯器將您的腳本翻譯成機器碼,使用 Matlab 提供的編譯器,您最多可以將速度提高 1.5 到 2 倍.

Matlab uses JIT compiler to translate your script to machine code, you can improve your speed at most by a factor 1.5 to 2 by using the compiler that Matlab provides.

Matlab 代碼可能能夠完全矢量化,但您必須在 C++ 中手動優(yōu)化代碼

完全矢量化的 Matlab 代碼可以調(diào)用用 C++/C/Assembly 編寫的庫(例如 Intel MKL).但是普通的 C++ 代碼可以被現(xiàn)代編譯器合理地向量化.

Fully vectorized Matlab code can call libraries written in C++/C/Assembly (for example Intel MKL). But plain C++ code can be reasonably vectorized by modern compilers.

Matlab 提供的工具箱和例程應(yīng)該經(jīng)過很好的調(diào)整并且應(yīng)該具有合理的性能

沒有.除線性代數(shù)例程外,性能普遍較差.

No. Other than linear algebra routines, the performance is generally bad.

與矢量化的 Matlab 代碼相比,您在 C++ 中可以獲得 10 倍~100 倍的性能的原因:

The reasons why you can gain 10x~100x performance in C++ comparing to vectorized Matlab code:

  1. 在 Matlab 中調(diào)用外部庫 (MKL) 需要花費時間.

  1. Calling external libraries (MKL) in Matlab costs time.

Matlab 中的內(nèi)存是動態(tài)分配和釋放的.例如小矩陣乘法:
A = B*C + D*E + F*G
需要 Matlab 創(chuàng)建 2 個臨時矩陣.而在 C++ 中,如果您事先分配內(nèi)存,則創(chuàng)建 NONE.現(xiàn)在想象你將該語句循環(huán)了 1000 次.C++ 中的另一個解決方案是由 C++11 Rvalue 參考提供的.這是C++最大的改進之一,現(xiàn)在C++代碼可以和普通C代碼一樣快了.

Memory in Matlab is dynamically allocated and freed. For example, small matrices multiplication:
A = B*C + D*E + F*G
requires Matlab to create 2 temporary matrices. And in C++, if you allocate your memory before hand, you create NONE. And now imagine you loop that statement for 1000 times. Another solution in C++ is provided by C++11 Rvalue reference. This is the one of the biggest improvement in C++, now C++ code can be as fast as plain C code.

如果要做并行處理,Matlab模型是多進程的,C++方式是多線程的.如果您有許多需要并行化的小任務(wù),C++ 可提供多達多個線程的線性增益,但在 Matlab 中您可能會獲得負面的性能增益.

If you want to do parallel processing, Matlab model is multi-process and the C++ way is multi-thread. If you have many small tasks needing to be parallelized, C++ provides linear gain up to many threads but you might have negative performance gain in Matlab.

C++ 中的向量化涉及使用內(nèi)在函數(shù)/匯編,有時 SIMD 向量化只能在 C++ 中實現(xiàn).

Vectorization in C++ involves using intrinsics/assembly, and sometimes SIMD vectorization is only possible in C++.

在 C++ 中,有經(jīng)驗的程序員可以完全避免 L2 緩存未命中甚至 L1 緩存未命中,從而將 CPU 推到其理論吞吐量極限.僅由于這個原因,Matlab 的性能可能會落后 C++ 10 倍.

In C++, it is possible for an experienced programmer to completely avoid L2 cache miss and even L1 cache miss, hence pushing CPU to its theoretical throughput limit. Performance of Matlab can lag behind C++ by a factor of 10x due to this reason alone.

在 C++ 中,計算密集型指令有時可以根據(jù)它們的延遲(在匯編或內(nèi)部函數(shù)中仔細編碼)和依賴性(大部分時間由編譯器或 CPU 硬件自動完成)進行分組,這樣理論上的 IPC(指令每個時鐘周期)可以達到并填充 CPU 管道.

In C++, computational intensive instructions sometimes can be grouped according to their latencies (code carefully in assembly or intrinsics) and dependencies (most of time is done automatically by compiler or CPU hardware), such that theoretical IPC (instructions per clock cycle) could be reached and CPU pipelines are filled.

然而,與 Matlab 相比,C++ 的開發(fā)時間也是 10 倍!

However, development time in C++ is also a factor of 10x comparing to Matlab!

你應(yīng)該使用 Matlab 而不是 C++ 的原因:

The reasons why you should use Matlab instead of C++:

  1. 數(shù)據(jù)可視化.我認為沒有 C++ 我的職業(yè)生涯可以繼續(xù),但沒有 Matlab 我將無法生存,因為它可以生成漂亮的繪圖!

  1. Data visualization. I think my career can go on without C++ but I won't be able to survive without Matlab just because it can generate beautiful plots!

低效率但數(shù)學上強大的內(nèi)置例程和工具箱.先得到正確答案再談效率.人們可以在 C++ 中犯一些細微的錯誤(例如將 double 隱式轉(zhuǎn)換為 int)并得到某種正確的結(jié)果.

Low efficiency but mathematically robust build-in routines and toolboxes. Get the correct answer first and then talk about efficiency. People can make subtle mistakes in C++ (for example implicitly convert double to int) and get sort of correct results.

表達您的想法并向您的同事展示您的代碼.Matlab代碼比C++更容易閱讀,也更短,而且Matlab代碼無需編譯器也能正確執(zhí)行.我只是拒絕閱讀其他人的 C++ 代碼.我什至不使用 C++ GNU 科學庫,因為無法保證代碼質(zhì)量.對于研究人員/工程師來說,將 C++ 庫用作黑匣子并將準確性視為理所當然是危險的.即使對于商業(yè) C/C++ 庫,我記得英特爾編譯器去年在其 sin() 函數(shù)中出現(xiàn)了sign 錯誤,并且 MKL 中也出現(xiàn)了數(shù)值精度問題.

Express your ideas and present your code to your colleagues. Matlab code is much easier to read and much shorter than C++, and Matlab code can be correctly executed without compiler. I just refuse to read other people's C++ code. I don't even use C++ GNU scientific libraries because the code quality is not guaranteed. It is dangerous for a researcher/engineer to use a C++ library as a black box and take the accuracy as granted. Even for commercial C/C++ libraries, I remember Intel compiler had a sign error in its sin() function last year and numerical accuracy problems also occurred in MKL.

使用交互式控制臺和工作區(qū)調(diào)試 Matlab 腳本比 C++ 調(diào)試器高效得多.在 Matlab 中查找索引計算錯誤可以在幾分鐘內(nèi)完成,但在 C++ 中,如果為了速度而刪除邊界檢查,則可能需要數(shù)小時才能弄清楚為什么程序會隨機崩潰.

Debugging Matlab script with interactive console and workspace is a lot more efficient than C++ debugger. Finding an index calculation bug in Matlab could be done within minutes, but it could take hours in C++ figuring out why the program crashes randomly if boundary check is removed for the sake of speed.

最后但并非最不重要的:

因為一旦 Matlab 代碼被向量化,程序員就沒有太多可以優(yōu)化的余地,所以與 C++ 代碼相比,Matlab 代碼性能對代碼質(zhì)量的敏感度要低得多.因此最好在 Matlab 中優(yōu)化計算算法,稍微好一點的算法通常在 Matlab 中的性能稍微好一點.另一方面,C++中的算法測試需要優(yōu)秀的程序員以相同的方式編寫或多或少優(yōu)化的算法,并確保編譯器不會以不同的方式優(yōu)化算法.

Because once Matlab code is vectorized, there is not much left for a programmer to optimize, Matlab code performance is much less sensitive to the quality of the code comparing with C++ code. Therefore it is best to optimize computation algorithms in Matlab, and marginally better algorithms normally have marginally better performance in Matlab. On the other hand, algorithm test in C++ requires decent programmer to write algorithms optimized more or less in the same way, and to make sure the compiler does not optimize the algorithms differently.

我最近在 C++ 和 Matlab 方面的經(jīng)驗:

去年做了幾個大型的Matlab數(shù)據(jù)分析工具,苦于Matlab速度慢.但是我能夠通過以下技術(shù)將我的 Matlab 程序速度提高 10 倍:

I made several large Matlab data analysis tools in the past year and suffered from the slow speed of Matlab. But I was able to improve my Matlab program speed by 10x through the following techniques:

  • 運行/分析 Matlab 腳本,在 C/C++ 中重新實現(xiàn)關(guān)鍵例程并使用 MEX 進行編譯.關(guān)鍵例程很可能在邏輯上很簡單,但在數(shù)字上很重.這將速度提高了 5 倍.

  • Run/profile the Matlab script, re-implement critical routines in C/C++ and compile with MEX. Critical routines are mostly likely logically simple but numerically heavy. This improves speed by 5x.

簡化.m"通過注釋所有不必要的安全檢查和輸出參數(shù)計算,來刪除隨 Matlab 工具箱一起提供的文件.請注意,修改后的代碼不能與其他用戶腳本一起分發(fā).這將速度再提高了 2 倍(在 C/C++ 和 MEX 之后).

Simplify ".m" files shipped with Matlab tool boxes by commenting all unnecessary safety checks and output parameter computations. Please be reminded that the modified code cannot be distributed with the rest of the user scripts. This improves speed by another 2x (after C/C++ and MEX).

改進后的代碼在 Matlab 中約為 98%,在 C++ 中約為 2%.

The improved code is ~98% in Matlab and ~2% in C++.

我相信如果整個工具用 C++ 編碼,速度可以再提高 2 倍(總共 20 倍),這是計算例程的約 100 倍速度提高.然后硬盤驅(qū)動器 I/O 將支配程序運行時間.

I believe it is possible to improve the speed by another 2x (total 20x) if the entire tool is coded in C++, this is ~100x speed improvement of the computation routines. The hard drive I/O will then dominate the program run time.

Mathworks 工程師的問題:

當 Matlab 代碼完全矢量化時,性能限制因素之一是矩陣索引操作.例如,需要對維度為 5000x5000 的矩陣 A 進行有限差分運算:

When Matlab code is fully vectorized, one of the performance limiting factor is the matrix indexing operation. For instance, a finite difference operation needs to be performed on Matrix A which has a dimension of 5000x5000:

B = A(:,2:end)-A(:,1:end-1)

矩陣索引操作使 Matlab 代碼比 C++ 代碼慢了數(shù)倍.能否提高矩陣索引性能?

The matrix indexing operation makes the Matlab code multiple times slower than the C++ code. Can the matrix indexing performance be improved?

這篇關(guān)于性能權(quán)衡 - MATLAB 何時比 C/C++ 更好/更慢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 91精品久久久久久久久久入口 | 91视频大全| 欧美精品一区二区三区四区 在线 | 久久久久久国产精品 | 97超在线视频| 欧美综合久久久 | 久久国产精品视频 | 日韩精品久久久久 | 天堂国产| 亚洲性视频在线 | 欧美精品一区二区三区在线播放 | 激情国产视频 | 成人夜晚看av| 亚洲精品视| 黄色日批视频 | 99久久久久 | 99视频在线看 | 91国在线观看 | 亚洲一区二区在线 | 欧美日韩在线精品 | avav在线看 | 国产黄色网 | 国产色| 亚洲欧美日韩一区二区 | 香蕉久久a毛片 | 玖玖视频网 | 欧美成人一区二区三区 | 免费一区| 暖暖成人免费视频 | 亚洲精品美女在线观看 | 精品国产乱码久久久久久1区2区 | 欧美精品成人 | 精品视频在线免费观看 | 国内精品久久精品 | av在线视 | 一区二区三区视频 | 中文字幕第90页 | 麻豆va| 国产一级在线 | 黄色一级大片在线免费看产 | 欧美男人天堂 |