'ABC'-'A' Considered Harmful
Mostra commenti meno recenti
The trick 'ABC'-'A' is that good programming style?
--- edit ---
"tag: answer" is a trace of the reason why I submitted the question. I've seen questions by "new to Matlab", which have received answers including not so obvious code with "'ABC'-'A'" embedded for no good reason.
3 Commenti
Jan
il 5 Mag 2012
The tag "cody" is a revelation. But what do "answer" and "goto" mean in this context?
Walter Roberson
il 7 Mag 2012
I have probably used this kind of expression from time to time in replying to people who might not be experienced in MATLAB.
In the cases where I was not adding or subtracting '0', the situation was likely one in which the lack of "good programming style" was intentional, such as cases where code was provided to prove that the task could be done, but where it "felt" likely to me that if plain code had been provided, the user would have copied the plain code for their assignment without attempting to understand it -- cases where if they blindly copied the more obscure code, any marker who actually read the code would immediately know that the person did not write the expression themselves.
per isakson
il 10 Mag 2012
Risposta accettata
Più risposte (4)
per isakson
il 13 Ago 2012
Modificato: per isakson
il 18 Lug 2016
1 Commento
James Tursa
il 17 Lug 2016
Modificato: James Tursa
il 17 Lug 2016
Interesting result. This appears to be the work of the parser optimizing stuff. E.g., from the command line:
>> S = char('A'+floor(rand(1,1e7)*25));
>>
>> clear a; tic; a = S - '0'; toc
Elapsed time is 0.068686 seconds.
>> clear a; tic; a = S - '0'; toc
Elapsed time is 0.068900 seconds.
>> clear a; tic; a = S - '0'; toc
Elapsed time is 0.064843 seconds.
>> clear a; tic; a = S - '0'; toc
Elapsed time is 0.059291 seconds.
>> clear a; tic; a = S - '0'; toc
Elapsed time is 0.070644 seconds.
>>
>> clear a; tic; a = double(S) - double('0'); toc
Elapsed time is 0.075243 seconds.
>> clear a; tic; a = double(S) - double('0'); toc
Elapsed time is 0.067354 seconds.
>> clear a; tic; a = double(S) - double('0'); toc
Elapsed time is 0.077742 seconds.
>> clear a; tic; a = double(S) - double('0'); toc
Elapsed time is 0.073420 seconds.
>> clear a; tic; a = double(S) - double('0'); toc
Elapsed time is 0.078540 seconds.
So at the command line the advantage of the double( ) disappears. Maybe there is some compiled code that MATLAB is using in the double( ) case that the parser has available, and no such compiled code exists for the character minus case.
Since this result seems to be the result of optimized code that the parser is able to use, I would not be surprised if this result varied quite a bit between MATLAB versions.
E.g., a simple mex routine result shows that the raw S - '0' calculation could be significantly improved with compiled code:
>> clear a; tic; a = char_minus(S,'0'); toc
Elapsed time is 0.044921 seconds.
>> clear a; tic; a = char_minus(S,'0'); toc
Elapsed time is 0.043699 seconds.
>> clear a; tic; a = char_minus(S,'0'); toc
Elapsed time is 0.046204 seconds.
>> clear a; tic; a = char_minus(S,'0'); toc
Elapsed time is 0.051441 seconds.
>> clear a; tic; a = char_minus(S,'0'); toc
Elapsed time is 0.058610 seconds.
The mex routine (bare bones, not production quality):
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mwSize i, n;
mwSize *dims;
mwSize ndim;
mxChar *cp;
mxChar c;
double *pr;
n = mxGetNumberOfElements(prhs[0]);
dims = mxGetDimensions(prhs[0]);
ndim = mxGetNumberOfDimensions(prhs[0]);
cp = mxGetChars(prhs[0]);
c = *mxGetChars(prhs[1]);
plhs[0] = mxCreateNumericArray(ndim, dims, mxDOUBLE_CLASS, mxREAL);
pr = mxGetPr(plhs[0]);
while( n-- ) {
*pr++ = *cp++ - c;
}
}
Jan
il 5 Mag 2012
It depends. The result is clear and well defined, but not obvious. If you store large arrays in an M-file, char occupies less memory in the RAM than double arrays. But storing large data sets in M-files is a bad programming style already, because this mixes data and program.
I use 'abc' - 'a' only to encode icons in M-files, because it allows a vague view of the result.
color = ['CCCCCHFFHCCCCC'; ...
'CDFNBFFFFFFFDC'; ...
'DPGGGGGGGGGGBH'; ...
'DPDMMMMOOAADPD'; ...
'DFFFNFFFFFFFIH'; ...
'CILLKJGEKNGEIC'; ...
'CILBKJLGKFNKIC'; ...
'CILBKJLGKFIKIC'; ...
'CILBKJLGKFIKIC'; ...
'CDLBKJLGKFIKDC'; ...
'CDLBKJLGKFIKDC'; ...
'CDLBKJLGKFIKDC'; ...
'CDLBKJLGKFNKDC'; ...
'CDLLKJGEKNGEDC'; ...
'CHBGEEEGGLPNHC'; ...
'CMDDIIDDDDDHMC'] - ('A' - 1);
map = [28, 26, 36; ...
116, 118, 132; ...
NaN, NaN, NaN; ...
73, 74, 89; ...
177, 176, 193; ...
96, 98, 112; ...
151, 152, 167; ...
60, 63, 76; ...
84, 84, 96; ...
220, 222, 236; ...
191, 193, 206; ...
135, 133, 143; ...
39, 41, 55; ...
108, 107, 118; ...
36, 34, 44; ...
124, 126, 140];
[x, y] = size(color);
Icon = reshape(map(color, :) / 255, x, y, 3);
uicontrol('Position', [10, 10, 32, 32], 'CData', Icon);
This is, in my opinion, the best way to store an icon in an M-file. But icons can be stored and edited much more comfortable in graphic files.
6 Commenti
per isakson
il 7 Mag 2012
Walter Roberson
il 7 Mag 2012
I do not agree on that point. The behavior of MATLAB of considering end-of-line inside a [] to be equivalent to a semi-colon is obscure and inconsistent with all other MATLAB syntax; having the semi-colon makes it easier to be sure of the intent.
Jan
il 7 Mag 2012
I always use as much separators as possible after there have been some inconsistencies between Matlab 4 and 5, as far as I remember. I avoid "1.0.*2.0.^3.0" also, and I never struggle with the differences between [1-2], [1 -2], [1- 2] and [1 - 2], because I prefer [1, -2] and if in doubt I even write [(1 - 2), 3] with unnecessary parenthesis.
I follow the policy, that white characters (space/linebreak/tab) should not carry a meaning outside of strings. This allows for an automatic detection of misplaced white characters by removing them and checking if the code is still consistent.
So I agree with Per, that "; ..." is a) not needed and b) does not add value. But it supports the debugging. According to the fact, that professional software developers using agile methods and a certified quality controlling insert about 1 bug per 1000 lines, any assistance for the debugging is welcome.
per isakson
il 10 Mag 2012
Modificato: per isakson
il 20 Set 2014
Geoff
il 10 Mag 2012
If MatLab used a single backslash for line continuation, I'd probably do it more often. =) I find the typing-in of three characters ...
on ...
every ...
line ...
quite ...
enervating.
Jan
il 10 Mag 2012
Btw., "..." does not only continuate the line, but starts a comment also. There is no need for an additional % and this is even documented.
Daniel Shub
il 5 Mag 2012
0 voti
I beleive that coding styles that sacrifice readibility for efficiency are generally bad style. It is possible that under some circumstances the gain in efficiency can offset the loss in readability. For example, in MATLAB loops used to be so slow that that we had to sacrifice readability for performance all the time by vectorizing everything. Thankfully that is not the case anymore.
Daniel Shub
il 9 Mag 2012
0 voti
I asked a similar question, although not identical by any means, a while back:
Categorie
Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!