Breaking up a computation vs "..." to continue line - Huge Speed Difference !?

2 ビュー (過去 30 日間)
Dan Ryan
Dan Ryan 2013 年 2 月 15 日
I was profiling an ode solver today and found something very strange. The system has 9 states and the computation of the ode RHS was taking about 100 times longer to compute the 5th state than any other state.
The 5th state equation was longer than the other equations, but not so drastically longer that it should take 100 times longer to compute. It ran over 4 lines in the editor so there were "..." continuations at the end of each line.
I rewrote the equation using multiple assignments instead of one big assignment and got about a 50 times speed up. By this, I mean I changed
value(5, :) = big_expression1 ...
+ big_expression2 ...
+ big_expression3 ...
+ big_expression4;
to
value(5,:) = big_expression1;
value(5,:) = value(5,:) + big_expression2;
value(5,:) = value(5,:) + big_expression3;
value(5,:) = value(5,:) + big_expression4;
Can you think of any reason why the second version should drastically outperform the first?
  1 件のコメント
Jan
Jan 2013 年 2 月 15 日
It depends on the real processing in "big_expression". Therefore your paraphrased code does not allow to reveal the problem.

サインインしてコメントする。

採用された回答

José-Luis
José-Luis 2013 年 2 月 15 日
編集済み: José-Luis 2013 年 2 月 15 日
I don't think it has anything to do with "...". Shot in the dark:
Cache locality? Maybe big_expression(n) and big_expression(n+1) fit together in the cache whereas when you call them all at the same time values need to be moved around in memory.
  2 件のコメント
Dan Ryan
Dan Ryan 2013 年 2 月 15 日
I would have never expected this to be a problem computing the rhs value of an ode state... just goes to show how helpful the profiler can be
per isakson
per isakson 2013 年 2 月 15 日
Did you find the reason to the big difference in performance?
I've done some experiments with R2012a (64bit) without seeing anything like it.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by