Help reduce the time.

조회 수: 2 (최근 30일)
Andrew
Andrew 2014년 8월 18일
댓글: Matz Johansson Bergström 2014년 8월 19일
This one line takes 85% %runtime.
Splitting it up increases %runtime by 5%. I have tried gpu (although my knowledge is very limited there).
Help please!
q is a vector, p a scalar.
q=[mod(q(1)*q(2),p^2) q(3:end)];
Thank you for any suggestions.
  댓글 수: 5
Andrew
Andrew 2014년 8월 18일
It is usually 1e4, but called around 4.7e6 times, so the overall script runs in 45s-50s, but I want to expand it so q=1e5. I was just wondering if there is ways that I can code this differently. After trying several different methods, it seems the majority of the work is done in the second line: q1=mod(q(1)*q(2),p^2); q=[q1 q(3:end)]; Why is this process so time consuming?
Thanks guys
Adam
Adam 2014년 8월 18일
Is p changing every time through the loop? If not then you should pull out p^2 to be calculated just once. I wouldn't expect that to make a huge difference though.

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

답변 (2개)

Matz Johansson Bergström
Matz Johansson Bergström 2014년 8월 18일
편집: Matz Johansson Bergström 2014년 8월 18일
On my computer it takes 0.0374 s (q = 1:10⁷). The code is essentialy copying data. I tried with just removing the second element
q(2) = []
but this takes 0.08 s , so this is not the way to do it. I instead noticed that we can write
tic
tmp = mod(q(1)*q(2), p^2);
q = q(2:end);
q(1) = tmp;
toc
The change is subtle but this takes 0.018 s, so it is approximately twice as fast.
Copying data is always a slow operation. Unless you really need to I would suggest you to keep the size of the vector and set elements to zero or something. Every time you take away the second element and shrink the vector, Matlab will copy the data.
To make sure we have done the best we can, consider the following
tic
f = q;
f(1) = f(1);
toc
This code copies the data from q to f and it takes 0.018 s , just as my suggestion.
OBS: In the above code I 'force' a copy by assigning f(1) to itself, otherwise Matlab will not copy until I use f later on. This is as fast you can get unless you change the way you handle the vector.
  댓글 수: 2
Andrew
Andrew 2014년 8월 18일
편집: Andrew 2014년 8월 18일
This is excellent. On a small q that reduced by computation from 0.089s to 0.070s. However, on a large q it has been reduced from 45s-50s to less than 30s! I have previously tired: q1=mod(q(1)*q(2),p^2); q=[q1 q(3:end)]; Why is this one slower than your version?
Cheers
Matz Johansson Bergström
Matz Johansson Bergström 2014년 8월 18일
I believe your version is forcing Matlab to evaluate and copy q twice.
I think that Matlab evaluates q(3:end), copies the data to a temporary vector (call it temp), then the slightly larger vector, [q1, temp] is copied to q. Matlab is thus interpreting the code and unfortunately cannot do a perfect job each time.
My version simply copies q(2:end) to q directly, once. Without that extra copying step.

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


Jan
Jan 2014년 8월 18일
It is not feasible to discuss about a speed improvement of a single line of code. Without seeing the context it is impossible to guess the reasons for the total runtime.
Di you have a good reason for the massive copying of data? I do not see a benefit to copy q(3:end). Why let q unchanged and store the modified values separately?
  댓글 수: 1
Matz Johansson Bergström
Matz Johansson Bergström 2014년 8월 19일
I agree and I also mentioned this in my answer.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by