[ ] or ( ) in constructing array
7 views (last 30 days)
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
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
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'.
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
Bruno Luong
on 16 Jul 2022
Edited: Bruno Luong
on 16 Jul 2022
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.
Walter Roberson
on 16 Jul 2022
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
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.
See Also
Categories
Find more on Logical 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!