Message Boards Message Boards

1
|
7945 Views
|
4 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Reduce the complexity of a code

Posted 10 years ago
POSTED BY: Fabio Rondelli
4 Replies
Posted 10 years ago

Dear Paul,

thank you very much for your kind reply. I am going to try with your advices and I will back to you for an update.

Thanks again Fabio

POSTED BY: Fabio Rondelli
Posted 10 years ago

Dear Fabio,

This may be of help with your problem, and as the Proff says AppendTo is very slow above a few thousand deep, I use the following Commands, Sow and Reap, the little snippet of code shows an example of its use.

Timing[vv = Reap[Do[Sow[{n, 2 n, 3 n}], {n, 1, 2000000}]]; 
 vv = Flatten[vv[[2]], 1]; Length[vv]]

{2.418016, 2000000}

Extracting the second part of the list removes the null statement part and flattening it reduces the 2 deep list to 1. Maybe this could be implemented in your code.

Paul.

POSTED BY: Paul Cleary
Posted 10 years ago

Dear Prof. Mutze,

thank you very much for your kind reply. It is a very good advice. I think that maybe it is possible to remove the AppendTo by defining an oversized array before the loop. I will work on it.
Please, if you have any other advice to reduce the code complexity, let me know, I am very grateful.

Many thanks again Fabio

POSTED BY: Fabio Rondelli

Dear Fabio, my experience (also the advice of experienced collegues) is that AppendTo is a performance-killer. If ever possible find the required array length first (probably a bit too large if only an upper bound for this length can be found) and then fill in the quantities which you allways append. The problem with appending is that it typically involves re-allocation of the array. Unfortunately I'm in a hurry and not able to work out whether this advice acan be followed in your case.

POSTED BY: Ulrich Mutze
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract