how to decrease the time consumption of the code

조회 수: 7 (최근 30일)
Abirami
Abirami 2014년 9월 4일
편집: Abirami 2014년 9월 4일
Hello I've already got a lot of help in developring the encryption technique from matlab answers. Now i have another problem. I need to reduce the time consumption of the folowing part of the code.Here i generate two chaotic sequences using Chen's hyperchaotic system. I've defined a function called Chen1.m and i've given both the codes below.This part of the code is taking almost 15 minutes.Please help.Thanks in advance.
%the values of a, b, c, d, k here
a = 36;
b = 3;
c = 28;
d = 16;
k = 0.2;
%vector v0, which is a 4x1 vector of initial conditions
v0 = [0.3 -0.4 1.2 1];
fun = @(t,v) chen1(t,v,a,b,c,d,k);
[t, v] = ode45(fun, [0 1.54], v0);
x = v(:,1);
x(257)=[];
y = v(:,2);
y(257)=[];
[lx,fx]=sort(x);
[ly,fy]=sort(y);
S=fx;
T=(fy)';
%%matrix generated for scrambling
[Tgrid, Sgrid] = meshgrid( T, S );
Z = arrayfun( @(S,T) [S T], Sgrid, Tgrid, 'UniformOutput', false );
%%scrambled matrix
F=cellfun(@(x) B{x(1),x(2)},Z,'un',0);
G=cellfun(@(x) E{x(1),x(2)},Z,'un',0);
The function is defined as follows
function vdot = chen(t, v, a, b, c, d, k)
x = v(1); y = v(2); z = v(3); q = v(4);
xdot = a*(y-x);
ydot = -(x*z)+(d*x)+(c*y)-q;
zdot = (x*y)-(b*z);
qdot = x+k;
vdot = [xdot; ydot; zdot; qdot];
end
  댓글 수: 1
Adam
Adam 2014년 9월 4일
First thing you should do is run the profiler on it:
profile on
functionCall(...)
profile off
profile viewer
That will tell you where the bottlenecks are. One of the worst things you can do when trying to speed up code is to start focusing on really speeding up a piece of code that only takes 1% of the total running time anyway.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2014년 9월 4일
Don't use cell arrays to hold B,E, F, and G. Cell arrays are slow and unnecessary for holding arrays of scalars. Once you've repaired the damage with
B=cell2mat(B)
E=cell2mat(E)
you should also replace this code segment
Z = arrayfun( @(S,T) [S T], Sgrid, Tgrid, 'UniformOutput', false );
%%scrambled matrix
F=cellfun(@(x) B{x(1),x(2)},Z,'un',0);
G=cellfun(@(x) E{x(1),x(2)},Z,'un',0);
with this
F=B(S,T);
G=E(S,T);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by