how to increase the run time of the matrix ?

조회 수: 2 (최근 30일)
navanit dubey
navanit dubey 2021년 3월 25일
편집: Jan 2021년 3월 25일
Hey ,
I am runnng a code for finding the shorter path , i am able to do it easily for matrix of 37*37 but as I increase its value it start to take lot of time.I tried to preallocate few values in it but not able to do it easily.
I am attaching 2 files for the reference. less_time.m is of 37*37 matrix and more_time.m is of 148*148 matrix.
kindly guide me how to preallocate the values in it so that I can increase the time of the operations
  댓글 수: 2
Jan
Jan 2021년 3월 25일
If runtime matters, omit the brute clearing header "clc; clear all; close all". It is useless inside a function, but removes all loaded funtions from the memory. Reloading them from the slow disk is a waste of time.
navanit dubey
navanit dubey 2021년 3월 25일
sure sir , I added them just to get the clean values in command window.
Thankyou for the response

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

답변 (2개)

Matt J
Matt J 2021년 3월 25일
The best option might be to use Matlab's own shortestpath routine,
  댓글 수: 3
Matt J
Matt J 2021년 3월 25일
Don't you mean you want to decrease the operation time? Don't you want it to be faster?
navanit dubey
navanit dubey 2021년 3월 25일
yes sir but I want that in my matlab file that I have attached to it as

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


Jan
Jan 2021년 3월 25일
편집: Jan 2021년 3월 25일
Some standard ideas for improving the runtime:
  • sqrt(X) is faster than X^0.5
  • The JIT acceleration is more powerful, if you write one command per line. Avoid constructs like this: "mink=k;minl=m;minkl=PLkm;"
  • Logical indexing is faster than numerical indexing.
% Replace:
Nonzero=find(PLK);
PLKPLK=PLK(Nonzero);
% by:
PLKPLK = PLK(PLK ~= 0);
  • This works, but the multiplication by 1 is confusing only, most of all in the combination with a lowercase L:
D = zeros(l*1,l*1);
% Nicer:
D = zeros(l, l); % But I avoid "l" consequently to reduce confusions
  • If you need 1 element only, do not waste time with searching all elements:
Select=find(Pcum>=rand);
to_visit=LJD(Select(1));
% Faster:
Select = find(Pcum>=rand, 1);
to_visit=LJD(Select);
  • In line 110 I assume you want a vector, not a matrix: "PP=zeros(Len_LJD);" Use PP=zeros(Len_LJD, 1) instead.
  • Then you can replace:
PP=zeros(Len_LJD);
for i=1:Len_LJD
PP(i)=(Tau(W,LJD(i))^Alpha)*((Eta(LJD(i)))^Beta);
end
sumpp=sum(PP);
PP=PP/sumpp;%Build probability distribution
Pcum(1)=PP(1);
for i=2:Len_LJD
Pcum(i)=Pcum(i-1)+PP(i);
end
Select=find(Pcum>=rand);
to_visit=LJD(Select(1));
% By:
PP = Tau(W,LJD).^Alpha .* Eta(LJD).^Beta;
PP = PP / sum(PP);
Pcum = cumsum(PP);
Select = find(Pcum >= rand, 1);
to_visit = LJD(Select);
  댓글 수: 2
navanit dubey
navanit dubey 2021년 3월 25일
편집: navanit dubey 2021년 3월 25일
Thank you so much sir for explaining each terms and for editing too. I have applied it to my matlab script.
Jan
Jan 2021년 3월 25일
편집: Jan 2021년 3월 25일
Now use the profiler to find out, where the most time is spent. This is the place to optimize the code to gain more speed.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by