MATLABOOP是慢的還是我做錯了什么?我在試驗MATLAB OOP,作為一個開始,我模仿了我的C+的Logger類,我把我所有的字符串助手函數(shù)都放在一個String類中,認為能夠這樣做是很棒的。a + b, a == b, a.find( b )而不是strcat( a b ), strcmp( a, b )的第一個元素。strfind( a, b )等問題:放緩我把上面的東西放了起來,立即注意到激烈減速。我是做錯了(這當然是可能的,因為我有相當有限的MATLAB經(jīng)驗),還是MATLAB的OOP只是引入了大量的開銷?我的測試用例下面是我對字符串所做的簡單測試,基本上只是追加一個字符串并再次刪除附加的部分:classdef String < handle
.... properties
stringobj = '';
end
function o = plus( o, b )
o.stringobj = [ o.stringobj b ];
end
function n = Length( o )
n = length( o.stringobj );
end
function o = SetLength( o, n )
o.stringobj = o.stringobj( 1 : n );
endendfunction atest( a, b ) %plain functions
n = length( a );
a = [ a b ];
a = a( 1 : n );function btest( a, b ) %OOP
n = a.Length();
a = a + b;
a.SetLength( n );function RunProfilerLoop( nLoop, fun, varargin )
profile on;
for i = 1 : nLoop
fun( varargin{ : } );
end
profile off;
profile report;a = 'test';aString = String( 'test' );RunProfilerLoop( 1000, @(x,y)atest(x,y), a, 'appendme' );RunProfilerLoop( 1000, @(x,y)btest(x,y), aString, 'appendme' );結(jié)果總時間(以秒為單位),用于1000次迭代:Btest 0.550(帶String.etLength 0.138,String.plus 0.065,String.Length 0.057)頂樓0.015記錄器系統(tǒng)的結(jié)果同樣是:對于1000次調(diào)用,結(jié)果為0.1秒frpintf( 1, 'test\n' ),7(!)在內(nèi)部使用String類時對我的系統(tǒng)的1000個調(diào)用的秒(好的,它有更多的邏輯,但是與C+相比:我的系統(tǒng)的開銷std::string( "blah" )和std::cout在輸出端對平原std::cout << "blah"是1毫秒左右。)在查找類/包函數(shù)時,它僅僅是開銷嗎?由于MATLAB是被解釋的,所以它必須在運行時查找函數(shù)/對象的定義。因此,我想知道,在查找類或包函數(shù)與路徑中的函數(shù)時,可能涉及到更多的開銷。我試過測試這個,結(jié)果變得更奇怪了。為了排除類/對象的影響,我將在路徑中調(diào)用函數(shù)與包中的函數(shù)進行比較:function n = atest( x, y )
n = ctest( x, y ); % ctest is in matlab pathfunction n = btest( x, y )
n = util.ctest( x, y ); % ctest is in +util directory, parent directory is in path所得結(jié)果與上述相同:每秒0.004秒,0.001秒0.060秒,0.014秒那么,所有這些開銷是來自MATLAB花費時間查找其OOP實現(xiàn)的定義,而對于直接在路徑中的函數(shù)來說,這種開銷并不存在嗎?
- 3 回答
- 0 關(guān)注
- 919 瀏覽
添加回答
舉報
0/150
提交
取消