The simple and same test program are run in matlab and VC6.0.I expect to get the conclusion that whether it is feasible to modify and optimize the current matlab code to improve the efficiency in order to make it can be run realtime in PC.
Case I research
In VC6.0, the program code is like this
// timetest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
int main(int argc, char* argv[])
{
DWORD Begin;
DWORD Gap;
long i,j,sum;
sum = 0;
Begin = GetTickCount();
for(i=0;i<1000001;i++)
{
for(j=0;j<21;j++)
{
sum = sum + 1;
}
}
Gap1 = GetTickCount() - Begin;
cout<< Gap1 <
}
Run the program and the elapsed time is 63ms
But if it is run in matlab, the code is like
tic;
sum = 0;
for i = 1:1000000
for j = 1:20
sum = sum * 1;
end
end
t = toc
fprintf('cost time is %d\n',t);
the running result is cost time is 26 s
so we can see
(1) the efficiency can be improved greatly, if the code is rewritten in C program.
(2) When we write Matlab program and have to use the nested loop, the time of outside loop use a small value can improve the running efficiency.
Case II
mcc method case I:
1. Using mcc –m myfun.m to produce the executive file myfun.
Case and efficiency analysis
//C function manually
#include "stdafx.h"
#include
#include
int main(int argc, char* argv[])
{
DWORD Begin;
DWORD Gap;
long i,j,sum;
sum = 0;
Begin = GetTickCount();
for(j=0;j<6;j++)
{
for(i=0;i<100000001;i++)
{
sum = sum + 1;
}
}
Gap = GetTickCount() - Begin;
cout<
}
Time spent in the C program manually is 1.5s
//mcc –m messay.m
function sum=messay()
tic;
sum = 0;
for j = 1:5
for i =1:1:100000000
sum = sum + 1;
end
end
tt = toc;
%messayfun(sum,tt);
fprintf('sum is %f,%d\n',sum,tt);
Time spent in the program is 5.01s
mcc method case II:
test3.m
function test3()
tic;
sum = 0;
for i = 1:10
for j = 1:1000000
sum = 2+1;
end
end
tt = toc;
fprintf('time cost test3 is %d\n',tt);
run time is 60.9ms
test3.m
(script file)
tic;
sum = 0;
for i = 1:10
for j = 1:1000000
sum = 2+1;
end
end
tt = toc;
fprintf('time cost test3 is %d\n',tt);
run time is 11s
From the the above result, we can make a conclusion that function can run faster than the script file.
test2.m
tic;
for i = 1:10
d = feval('messayfun',2,3);
end
tt = toc;
fprintf('time cost is %d\n',tt);
messayfun.m
function c = messayfun(a,b)
for j=1:1000000
c = a+b;
end
time cost is 61.4ms
#include "stdafx.h"
#include
#include
int main(int argc, char* argv[])
{
DWORD Begin;
DWORD Gap;
long i,j,sum;
sum = 0;
Begin = GetTickCount();
for(j=0;j<11;j++)
{
for(i=0;i<1000001;i++)
{
sum = sum + 1;
}
}
Gap = GetTickCount() - Begin;
cout<
}
It can be seen that the time cost with C program can be decreased 50%.
Conclusion:
1. “feval” function’s efficiency is high.
2. The most efficient method is C program and it need to be written to replace the loop part in matlab program. The efficiency can be increased to twice.
Currenly, Matlab program optimality method is very good.The efficiency can be increased to twice if the C code program is used. It’s impossible to expect the program efficiency increase more 300 times(in order to run the algorithm realtime) only depended on transferring matlab program to C program. The feasible method is decreasing the particle number, decrease the state variable, or modify the PF method.
没有评论:
发表评论