[ ] or ( ) in constructing array
Show older comments
In constructing array, we can have different styles as show below. Personally, I found that using [ ] is a better choice in MATLAB since it reminds that an array is created. Using ( ) tends to give an impression that an expression or function arguments are going to used when you quickly browse through the code.
MATLAB code analyser in the editor gives a warning message when using statement such as b=[1:2:300000] indicating that [ ] are not necessary.
I am wondering if there is a performance penalty using [ ] for constructing array or any disadvantages for discouraging the use of [ ]. It is also a bit annoying the code analyser give an warning on this if there is nothing wrong to use [ ].
% Style 1 using ( )
tic
for i=1:1e3
a=(1:2:300000);
end
toc
% Style 2 using [ ]
tic
for i=1:1e3
b=[1:2:300000];
end
toc
% Style 3
tic
for i=1:1e3
a=1:2:300000;
end
toc
7 Comments
Stephen23
on 16 Jul 2022
Chunru
on 17 Jul 2022
Chunru
on 17 Jul 2022
Rik
on 17 Jul 2022
I try to assume good faith, especially for frequent contributors. Do you still think it is unethical to delete an answer if it is an attempt at de-escalation?
I will have a look at this thread later to see if I can voice my thoughts in a new answer without fanning the flames.
Chunru
on 17 Jul 2022
Bruno Luong
on 19 Jul 2022
Edited: Bruno Luong
on 19 Jul 2022
Chunru
on 20 Jul 2022
Answers (4)
Bruno Luong
on 16 Jul 2022
Edited: Bruno Luong
on 16 Jul 2022
The only danger is that unexpected results when someone is careless about indentation and spacing
a = 1 : 5 +2
b = [1 : 5 +2]
c = (1 : 5 +2)
Steven Lord
on 16 Jul 2022
The fastest and most bug-free piece of code you can write is the one that doesn't exist because it doesn't need to exist.
When I see someone write a piece of code like:
x = [1:10]
I get a little tingling in my nose, as this is a (very mild but still detectable) code smell. Did the author of that code intend to concatenate something together with that 1:10, and so this line has a bug? What are they trying to convey with those square brackets above and beyond what's conveyed by that colon operator? You said "since it reminds that an array is created." but IMO that's not what reminds the reader of the code that an array is created. The : does.
13 Comments
Bruno Luong
on 16 Jul 2022
Edited: Bruno Luong
on 16 Jul 2022
but IMO that's not what reminds the reader of the code that an array is created. The : does.
The "IMO" is important.
The ":" can create a very specific type of array: one row with equidistance between two adjacent elements, with "[]" one can create any type of array in theory.
And now you know that your view is not universally shared Steve.
Oh, I already knew that. Perhaps I should have said that in this context it's not the [] that indicates array creation, it's the colon. In other contexts commas or spaces (to separate elements in a row) or semicolons or line breaks (to separate rows) are indicators of array creation. In others it's the presence of the cat function.
x = cat(3, 1, 2, 3)
"with "[]" one can create any type of array in theory. "
No, square brackets cannot concatenate along the third or higher dimensions.
"And now you know that your view is not universally shared Steve."
And yet ironically you highlight the exact problem with the OPs "style": it is precisely because square brackets are used to concatenate horizontally and/or vertically (but not along higher dimensions) that using them to concatenate one array with nothing else is misleading: the OP is not "creating any kind of array" using the square brackets, for the simple fact that the array already exists.
Bruno Luong
on 16 Jul 2022
Edited: Bruno Luong
on 16 Jul 2022
You still missing the point, some people prefer to *highlight* an array with []; The only purpose is the subjective "style" that some people might prefer. We don't use it because we expect it to do something superfluous in the computation (or because we are dumb in MATLAB to your view), we use it to help us to read the code.
A = [ [0], [1,2,4];
[1;
2;
4], [zeros(3)] ]
The above might be completely awful to your highe-level MATLAB expertise, to me I see better the block structures of my matrix, sometime closer to a notation of some book or paper.
Steven Lord
on 16 Jul 2022
Fair enough. Code Analyzer doesn't block you from doing that, it simply says there may be an issue or an inefficiency here.
But if you are really annoyed with Code Analyzer for doing that, you can suppress that particular message. The "Adjust Code Analyzer Message Indicators and Messages" section on this documentation page states how you can suppress a message on that particular line, in the whole file, or in all files.
Chunru
on 17 Jul 2022
"You still missing the point, some people prefer to *highlight* an array with [];"
I am perfectly aware that the OP wants to "highlight" an array, however the the OP's question title is not "[ ] or ( ) in highlighting an array", but is in fact "[ ] or ( ) in constructing array", and then it procedes to repeat this incorrect staement, e.g. " ...is a performance penalty using [ ] for constructing array" when in fact none of their examples or given use-cases related to this question actually use square brackets for constructing arrays. That is the sole point that I am making.
Can someone please show me how I can use "( ) in constructing array" , as the title states?
" But if there is no harm..."
There clearly is a harm: a large number of other users who find that style misleading.
"...why not use it for your benefit"
You are not just using that misleading "style" for "your benefit", but also on this forum.
"to me I see better the block structures of my matrix, sometime closer to a notation of some book or paper."
Of course, if in a specific situation it makes code clearer, then by all means use it (just like any MLINT message, which can be ignored when the situations requires it). But the original question is not asking about specific "notation of some book or paper", it asked for a general advice on a general coding style, to be applied every single time the author creates an array using the colon operator.
Including superfluous code as a general rule is not suported by your comment (in specific situations -> decide to ignore MLINT).
Chunru
on 17 Jul 2022
Steven Lord
on 17 Jul 2022
It feels like this thread is getting a bit heated. How about we all take a little break from it, say 24 hours, and then if there's still stuff to be said on this topic come back a little calmer? Thanks.
Chunru
on 17 Jul 2022
Rik
on 17 Jul 2022
@Chunru your posts still come across to me as very aggressive. To such a degree in fact that I don't feel my opinion would be treated as opinion, but instead as a personal attack.
Please be aware that there may be linguistic and cultural barriers. I have not seen attempts to monopolize the discussion (most users here have sufficient moderation rights to completely edit away any dissent). As to the bullying and harassment; it is difficult to judge the intent from text alone. Given the long history of most users in this thread, I am inclined to think they do not intend their posts to be interpreted like that. That is what I meant by 'frequent contributor'.
Chunru
on 17 Jul 2022
Walter Roberson
on 16 Jul 2022
just use
b = 1:2:300000;
When you use [] then the call would be equivalent to
b = horzcat(1:2:300000);
which contains an unnecessary function call and can potentially remove an opportunity for optimization
1 Comment
Really?
foo
function y = foo
dbstop in horzcat
x = pi;
a = [1:10];
y = a + x;
dbclear all
end
A don't have any stop triggered when I run foo. with the "superflous" [] The code analyzer and engine are smart enough so horzcat is not explicitly called. It is really stupid if the mlint is more intelligent than the engine.
I am wondering if there is a performance penalty using [ ] for constructing array
If you measure inside a function, and if you discard the first 10-ish measurements (which typically have peak times that are substantially higher, for reasons I do not fully understand), then the time using [] is sometimes as low as the time without [], but typically peaks 6 to 8 percent higher.
N = 50;
t1 = zeros(N,1);
t2 = zeros(N,1);
for K = 1 : N
S = tic; without(); ; E = toc(S); t1(K) = E;
S = tic; with(); E = toc(S); t2(K) = E;
end
t1(1:10) = []; t2(1:10) = [];
plot([t1,t2]); legend({'no []', '[]'});
mean(t1), std(t1)
mean(t2), std(t2)
function without()
data = 1:2:300000;
end
function with()
data = [1:2:300000];
end
10 Comments
Bruno Luong
on 17 Jul 2022
Increase N to 500, this is the result on my computer R2022a. It looks to me there is hardly any difference.

Walter Roberson
on 17 Jul 2022
I have noticed distinct timing pattern differences between Answers and my desktop. A possibility is that timing patterns might hypothetically be different for Linux than for Mac
Paul
on 17 Jul 2022
Is Matlab obligated to actually execute the assignment to data in with() and without()? data is not used, and the intepreter could detect that there aren't any side effects from evaluating the right hand side. From what I can tell, it actually does perform the assignment.
Bruno Luong
on 17 Jul 2022
Edited: Bruno Luong
on 17 Jul 2022
@Paul I mofify Walter's code and returns data to main script. The tic/toc result is sensible identical.
Is Matlab obligated to actually execute the assignment to data in with() and without()?
It is if you return the value. The only documented optimization between calling levels has to do with a case where data can be updated "in place" instead of using copy-on-write.
There are reasons why you might want to waste time, so optimization should not remove a chain that does not affect output... but there are cases where code might not be executed exactly as written, if there is a code pattern that is implemented by one of the high speed libraries.
N = 100;
t1 = zeros(N,1);
t2 = zeros(N,1);
for K = 1 : N
S = tic; without(); ; E = toc(S); t1(K) = E;
S = tic; with(); E = toc(S); t2(K) = E;
end
t1(1:10) = []; t2(1:10) = [];
plot([t1,t2]); legend({'no []', '[]'});
mean(t1), std(t1)
mean(t2), std(t2)
function data = without()
data = 1:2:300000;
end
function data = with()
data = [1:2:300000];
end
@Bruno Luong, I had did the opposite, i.e. commented the assignment from with() so that that with() is basically a no-op, and saw a timing change, which is how I concluded the assignment, though unneccessary, was actually happening.
@Walter Roberson, For me, I might want Matlab to optimize away that assignment every time. If I want code to waste time (I never do but realize that others might) I'm sure there must be better ways, like pause() or something. OTOH, I'm not sure how such optimization would interact with the debugger and profiler, so maybe (probably) there's much more to that kind of optimization than meets my eye.
Interestingly, at least to me, Matlab creates the array even with this line at the command prompt
tic, for ii = 1:5000,1:2:300000;end,toc
tic, for ii = 1:5000, end,toc
Bruno Luong
on 17 Jul 2022
Edited: Bruno Luong
on 17 Jul 2022
May be because the reason is that when you do an expression without assign to a variable, matlab still must assign to ANS, so all expression without user assigment must be still evaluated.
Walter Roberson
on 17 Jul 2022
There are some cases where an assignment of a literal colon expression will copy the results into a hidden variable, and then on later lines that use the exact same literal colon expression including spacing before any comment, will use the shadow variable. The boundary is on the order of 4 kilobytes if I recall correctly.
Unfortunately I have had difficulty locating the relevant discussion. I seem to recall that James Tursa raised the issue, having noticed that altering an element of a colon expression within a mex routine also affected a different colon expression.
Walter Roberson
on 17 Jul 2022
"Busy work" to delay cannot be completely replaced by pause()
- the resolution of pause is 1/100 s or 1/1000 s (Mac) which is often not good enough
- pause() is handled by operating system calls, which give up control of the core. When using hyperthreads the other thread gets control over the core until it volunteers to give up control. When not using hyperthreads, giving up the core means that when the timer expires in the kernel, your process gets scheduled for execution, with an indefinite delay before execution depending on operating system policy and competition for resources
- generally speaking, delays in the kernel show up quite differently for side-channel and differential analysis for the purposes of covert attacks. On the other hand, I am not convinced that MATLAB is suitable for writing software intended to resist those attacks.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
