An open exchange for the MATLAB and Simulink user community |
Hosted by The MathWorks |
|
| Related Topics |
| New Products | Support | Documentation | Training | Webinars | Jobs | Newsletters |
| Problems? Suggestions? Contact us at files@mathworks.com | © 1994-2008 The MathWorks, Inc. Trademarks Privacy Policy |
Doug - I have found that 2-d geometry problems are best solved in complex coordinates
zc = xc+i*yc;
z = x+i*y;
e = z-zc;
if abs(e)>1 & abs(real(e))
Terry,
Thanks for the comment.
As you can see, this code was far from efficient so that I would have something to improve upon. I was not planning on converting the code to imaginary coordinates, but for a more realistic problem, that is an good idea.
It looks like the code is cleaner your way. I was planning on focusing on the benefits of good variable names, breaking down logic checks and the like. The actual problem domain is not important, so I was not going to make domain specific code changes.
Thanks,
Doug
We have a mess code but is efficient an we have a clean code but loses in speed. Which code we prefer?
In your example which code is faster? The first or the clean version?
ps: I know, that if we have clean code we can do debugging very easy.But i dont know if sometimes we earn something else
I have the same question as Nick. My solution has generally been to write the complicated code, and then provide the “simple” code as a comment, so that I don’t have to give up speed. I also wonder about the merits of cluttering up the variable name space… Is this yet another case, “there is never really one size that fits all”?
Dan and Nick,
There are times when cryptic code code does give faster results. The profiler, as outlined in an earlier video helps to diagnose that. However, I think that sloppy code, like the kind in my original file, is rarely faster, simply harder to understand. Even if the clearer code were slower, the ease of understanding would outweigh most small performance gains in most situations.
That being said, I think it is worth adding another video at the end to compare the two. My instinct is that their will be negligible difference in speed.
Doug
Doug,
I’m thinking mainly of the extra time required in memory allocation and handling, when you are processing large volumes of data. What I am really unsure of here, is what memory usage MatLab makes for intermediate steps in complex computations. Is it allocating the memory just the same as it would if we assigned each intermediate value to a variable and then clearing that allocation at the end of the computation? If so then I would imagine you really don’t lose much time. For reference I’m thinking about processing done on arrays that are, for example 20,000×25, which can definitely chew up a bunch of memory.
Dan
Dan-
With respect to temporary expressions, MATLAB cleans up or reuses the memory as it goes. So, for example, assuming all variables and sizes are suitable for this expression
(a+b)+(c+d)
We end up with 2 intermediate temporaries (not 3). The temporaries start off being for the expressions in parentheses. And when those subexpressions are added, MATLAB is smart enough to know that it can overwrite one of the temps with the final value. So at most here, you have only one extra temporary).
There are some extra considerations that MATLAB does for expressions like
A = A + 1;
when this is inside a function M-file. I will be writing about that soon in one of my upcoming blog articles.
–Loren
Loren,
Thanks for the answer. From that response it does sound like the complicated, ugly, code probably does save a lot of memory allocation, at least when you are dealing with large sets of data.
As a side note, it would be interesting if profiler had a mechanism which allowed one to know what part of an expression was using time, rather than just line by line. I’m guessing that that is a far from simple modification, however.
Dan
dear sir
do you have a NSGA-II coded in matlab with binary code
thanks
File exchange to the rescue:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10429&objectType=File
Thank indeed for ur help but I’m looking for NSGA-II with binary coded (representation) the one that have u sent if real vector
Hi sebaa,
Have u found a NSGA-II coded in matlab with binary code