Mex and Matlab Code
1 次查看(过去 30 天)
显示 更早的评论
Using the Concept from
I have done :
function out = sum_it(a,b)
out = a + b;
>> codegen -args {2.0 3.0} sum_it.m
My Test Function :
ip1 = rand(100,1);
ip2 = rand(100,1);
for j = 1:20
tic()
for i = 1:len
Pa(i) = sum_it(ip1(i), ip2(i));
end
a(j) = toc();
tic()
for i = 1:len
Pb(i) = sum_it_mex(ip1(i), ip2(i));
end
b(j) = toc();
fprintf('%f %f\n',mean(a),mean(b))
end
Unfortunately a < b. How can it be so ??? I was expecting b <<< a by big margin ! What am I doing wrong?
0.004559 0.004008
0.002347 0.003053
0.001587 0.002737
0.001214 0.002609
0.000989 0.002450
0.000834 0.002305
0.000728 0.002175
0.000645 0.002064
0.000580 0.002015
0.000534 0.001983
0.000491 0.002014
0.000458 0.002048
0.000430 0.002079
0.000406 0.002092
0.000386 0.002094
0.000369 0.002105
0.000353 0.002124
0.000338 0.002144
0.000326 0.002146
0.000313 0.002114
回答(1 个)
Ryan Livingston
2015-4-27
Generally what is happening here, is that the body of sum_it calls a MATLAB built-in function plus. Such functions are already implemented in compiled code in MATLAB and generating code for them is not expected to accelerate them significantly. The documentation for acceleration describes this more under the heading Avoid Generating MEX Functions if Built-In MATLAB Functions Dominate Run Time.
The likely reason for the small slowdown is that calling a MEX function has some overhead. Because the body of sum_it is so simple, that overhead is dominating the small execution time.
If you have a larger application that you would like to accelerate, then I would suggest following the steps on that documentation page that describe using the profiler to identify the bottlenecks in your code. Once those are identified, then you can determine if they are good candidates for acceleration via MATLAB Coder and follow the steps described to generate code for those small portions.
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!