-
7 Comments
I’ve found that the huge strength of MATLAB is that it frees me to rapidly prototype numerical math problems – before I design a targeted solution. Such esoteric efficiencies as above are very clever and much credit goes to the person who presented it, however, this distracts rather than adds significant value to my use of MATLAB. Depending on the target machine (Desktop workstation or embedded device) I will ultimately use C/C++, assembly and VHDL to implement my process efficient code – never an interpreted language.
This is an update since I had one person ask about my post...
I believe that speed is only one metric of performance, another is memory utilization. The key in this problem is the “type” of the variable stored internally. By using ans=1:10; the variable ans is (I believe) of the type double precision floating point (8 Bytes for each number). On the other hand by using str2num, a cast to an array of Characters (1 Byte each) is used to tightly store the tiny integer range of numbers needed. Thus the memory required to store ans is 8 times less.
SO, I’m not sure how they actually score (or weight) the importance between memory efficiency and execution time (and across multiple platforms and OS’s) since in your case you claim a faster execution time without the casting. This would need to be asked to a Mathworks person with specific insight to their code – sorry.
And back to my soapbox point… I’m using a machine that has 12 GB of RAM, so the difference between using 10 bytes or 80 bytes is not important to me.
HOWEVER, understanding this issue IS important so my hat is off to the Cody folks for the great (and fun) challenge!
Hope that helps!
john
The metric used to evaluate the performance of a solution is http://www.mathworks.com/matlabcentral/fileexchange/34754-calculate-size
As you can see if you inspect the code, it has nothing to do with speed or memory. That is why I proposed this solution, in order to show that you can become the leader with a code that is not efficient at all, either in execution speed or in memory use. Although I think the idea of Cody is great and challenging I believe that the metric that is used nowadays should be rethought.
interesting
Cannot be reproduced, creates an error in execution in R2016a.
I noticed a lot of leading solutions are using 'str2num' or regexp functions, though codes can be written in a simpler and shorter way without invoking these functions. e.g., in this case, 1:10; would have done trick but still, it shows a larger size compared to the above solution. can anyone explain?
@Imran it's a quirk of the Cody scoring system. A solution's size is the node count in the internal MATLAB representation of the code submitted. Try running m = mtree(fileread("oneToTen.m")); in the console; then m.dumptree will show you the parse tree, and m.count will show you the number of nodes, i.e. the score it'll get on Cody. The reason that this achieves a lower size than more direct solutions can be found by comparing the parse trees.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!