# how to decrease the time consumption of the code

2 Ansichten (letzte 30 Tage)
Abirami am 4 Sep. 2014
Bearbeitet: Abirami am 4 Sep. 2014
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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
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.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Matt J am 4 Sep. 2014
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 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Adding custom doc finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by