pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問(wèn)題描述

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

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.

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

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.

當(dāng)我繼續(xù)使用數(shù)千行代碼進(jìn)行更大規(guī)模的模擬時(shí),慢慢地就會(huì)出現(xiàn)真實(shí)的畫(huà)面.C++ 仿真顯示出卓越的性能,在時(shí)間復(fù)雜度上比等效的 MATLAB 實(shí)現(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ù)的,并且沒(méi)有明確進(jìn)行高保真優(yōu)化.然而,據(jù)我所知,MATLAB 本質(zhì)上做了很多優(yōu)化.例如,當(dāng)我嘗試生成大量隨機(jī)樣本時(shí),就會(huì)出現(xiàn)這種情況,而在 C++ 中,使用 IT++/GSL/Boost 等庫(kù)的等效項(xiàng)的執(zhí)行速度相對(duì)較慢(使用的算法相同,即 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).

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

順便說(shuō)一句,我認(rèn)為這里的編碼效率參數(shù)并不重要,在這兩種情況下考慮的是同一個(gè)程序員.而且,我認(rèn)為其他替代品如 python,R 在這里不相關(guān).但是對(duì)我們使用的特定庫(kù)的依賴(lài)應(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ìn)行模擬,并且在兩種情況下都編寫(xiě)了幾萬(wàn)行代碼的合理經(jīng)驗(yàn)]

推薦答案

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

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

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

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

Matlab 使用 JIT 編譯器將您的腳本翻譯成機(jī)器碼,使用 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++ 中手動(dòng)優(yōu)化代碼

完全矢量化的 Matlab 代碼可以調(diào)用用 C++/C/Assembly 編寫(xiě)的庫(kù)(例如 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)過(guò)很好的調(diào)整并且應(yīng)該具有合理的性能

沒(méi)有.除線性代數(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)用外部庫(kù) (MKL) 需要花費(fèi)時(shí)間.

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

Matlab 中的內(nèi)存是動(dòng)態(tài)分配和釋放的.例如小矩陣乘法:
A = B*C + D*E + F*G
需要 Matlab 創(chuàng)建 2 個(gè)臨時(shí)矩陣.而在 C++ 中,如果您事先分配內(nèi)存,則創(chuàng)建 NONE.現(xiàn)在想象你將該語(yǔ)句循環(huán)了 1000 次.C++ 中的另一個(gè)解決方案是由 C++11 Rvalue 參考提供的.這是C++最大的改進(jìn)之一,現(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模型是多進(jìn)程的,C++方式是多線程的.如果您有許多需要并行化的小任務(wù),C++ 可提供多達(dá)多個(gè)線程的線性增益,但在 Matlab 中您可能會(huì)獲得負(fù)面的性能增益.

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ù)/匯編,有時(shí) SIMD 向量化只能在 C++ 中實(shí)現(xiàn).

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

在 C++ 中,有經(jīng)驗(yàn)的程序員可以完全避免 L2 緩存未命中甚至 L1 緩存未命中,從而將 CPU 推到其理論吞吐量極限.僅由于這個(gè)原因,Matlab 的性能可能會(huì)落后 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ì)算密集型指令有時(shí)可以根據(jù)它們的延遲(在匯編或內(nèi)部函數(shù)中仔細(xì)編碼)和依賴(lài)性(大部分時(shí)間由編譯器或 CPU 硬件自動(dòng)完成)進(jìn)行分組,這樣理論上的 IPC(指令每個(gè)時(shí)鐘周期)可以達(dá)到并填充 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++ 的開(kāi)發(fā)時(shí)間也是 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ù)可視化.我認(rèn)為沒(méi)有 C++ 我的職業(yè)生涯可以繼續(xù),但沒(méi)有 Matlab 我將無(wú)法生存,因?yàn)樗梢陨善恋睦L圖!

  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ù)學(xué)上強(qiáng)大的內(nèi)置例程和工具箱.先得到正確答案再談效率.人們可以在 C++ 中犯一些細(xì)微的錯(cuò)誤(例如將 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.

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

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.

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

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.

最后但并非最不重要的:

因?yàn)橐坏?Matlab 代碼被向量化,程序員就沒(méi)有太多可以?xún)?yōu)化的余地,所以與 C++ 代碼相比,Matlab 代碼性能對(duì)代碼質(zhì)量的敏感度要低得多.因此最好在 Matlab 中優(yōu)化計(jì)算算法,稍微好一點(diǎn)的算法通常在 Matlab 中的性能稍微好一點(diǎn).另一方面,C++中的算法測(cè)試需要優(yōu)秀的程序員以相同的方式編寫(xiě)或多或少優(yōu)化的算法,并確保編譯器不會(huì)以不同的方式優(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)驗(yàn):

去年做了幾個(gè)大型的Matlab數(shù)據(jù)分析工具,苦于Matlab速度慢.但是我能夠通過(guò)以下技術(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:

  • 運(yùn)行/分析 Matlab 腳本,在 C/C++ 中重新實(shí)現(xiàn)關(guān)鍵例程并使用 MEX 進(jìn)行編譯.關(guān)鍵例程很可能在邏輯上很簡(jiǎ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.

簡(jiǎn)化.m"通過(guò)注釋所有不必要的安全檢查和輸出參數(shù)計(jì)算,來(lái)刪除隨 Matlab 工具箱一起提供的文件.請(qǐng)注意,修改后的代碼不能與其他用戶(hù)腳本一起分發(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).

改進(jìn)后的代碼在 Matlab 中約為 98%,在 C++ 中約為 2%.

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

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

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 工程師的問(wèn)題:

當(dāng) Matlab 代碼完全矢量化時(shí),性能限制因素之一是矩陣索引操作.例如,需要對(duì)維度為 5000x5000 的矩陣 A 進(jìn)行有限差分運(yùn)算:

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 何時(shí)比 C/C++ 更好/更慢的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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++ 中,為什么我不能像這樣編寫(xiě) 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)形?)
主站蜘蛛池模板: 机械立体车库租赁_立体停车设备出租_智能停车场厂家_春华起重 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 电缆故障测试仪_电缆故障定位仪_探测仪_检测仪器_陕西意联电气厂家 | 水轮机密封网 | 水轮机密封产品研发生产厂家 | 云南丰泰挖掘机修理厂-挖掘机维修,翻新,再制造的大型企业-云南丰泰工程机械维修有限公司 | 风淋室生产厂家报价_传递窗|送风口|臭氧机|FFU-山东盛之源净化设备 | 磁力加热搅拌器-多工位|大功率|数显恒温磁力搅拌器-司乐仪器官网 | 不锈钢发酵罐_水果酒发酵罐_谷物发酵罐_山东誉诚不锈钢制品有限公司 | 伊卡洛斯软装首页-电动窗帘,别墅窗帘,定制窗帘,江浙沪1000+别墅窗帘案例 | 动物解剖台-成蚊接触筒-标本工具箱-负压实验台-北京哲成科技有限公司 | jrs高清nba(无插件)直播-jrs直播低调看直播-jrs直播nba-jrs直播 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 济南货架定做_仓储货架生产厂_重型货架厂_仓库货架批发_济南启力仓储设备有限公司 | ICP备案查询_APP备案查询_小程序备案查询 - 备案巴巴 | 广州办公室设计,办公室装修,写字楼设计,办公室装修公司_德科 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 老城街小面官网_正宗重庆小面加盟技术培训_特色面馆加盟|牛肉拉面|招商加盟代理费用多少钱 | 智能终端_RTU_dcm_北斗星空自动化科技| 存包柜厂家_电子存包柜_超市存包柜_超市电子存包柜_自动存包柜-洛阳中星 | 海尔生物医疗四川代理商,海尔低温冰箱四川销售-成都壹科医疗器械有限公司 | 新疆系统集成_新疆系统集成公司_系统集成项目-新疆利成科技 | MVE振动电机_MVE震动电机_MVE卧式振打电机-河南新乡德诚生产厂家 | 车间除尘设备,VOCs废气处理,工业涂装流水线,伸缩式喷漆房,自动喷砂房,沸石转轮浓缩吸附,机器人喷粉线-山东创杰智慧 | 电机修理_二手电机专家-河北豫通机电设备有限公司(原石家庄冀华高压电机维修中心) | 右手官网|右手工业设计|外观设计公司|工业设计公司|产品创新设计|医疗产品结构设计|EMC产品结构设计 | 上海网站建设-上海网站制作-上海网站设计-上海做网站公司-咏熠软件 | 银川美容培训-美睫美甲培训-彩妆纹绣培训-新娘化妆-学化妆-宁夏倍莱妮职业技能培训学校有限公司 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 一体化污水处理设备-一体化净水设备-「山东梦之洁水处理」 | 防腐木批发价格_深圳_惠州_东莞防腐木厂家_森源(深圳)防腐木有限公司 | 铝箔袋,铝箔袋厂家,东莞铝箔袋,防静电铝箔袋,防静电屏蔽袋,防静电真空袋,真空袋-东莞铭晋让您的产品与众不同 | 薪动-人力资源公司-灵活用工薪资代发-费用结算-残保金优化-北京秒付科技有限公司 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 对照品_中药对照品_标准品_对照药材_「格利普」高纯中药标准品厂家-成都格利普生物科技有限公司 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 | 天津力值检测-天津管道检测-天津天诚工程检测技术有限公司 | 电车线(用于供电给电车的输电线路)-百科 | 储气罐,真空罐,缓冲罐,隔膜气压罐厂家批发价格,空压机储气罐规格型号-上海申容压力容器集团有限公司 | LED太阳能中国结|发光红灯笼|灯杆造型灯|节日灯|太阳能灯笼|LED路灯杆装饰造型灯-北京中海轩光电 | ICP备案查询_APP备案查询_小程序备案查询 - 备案巴巴 | CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 |