[ ] or ( ) in constructing array

12 views (last 30 days)
Chunru
Chunru on 16 Jul 2022
Commented: Chunru on 20 Jul 2022
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
Elapsed time is 0.179663 seconds.
% Style 2 using [ ]
tic
for i=1:1e3
b=[1:2:300000];
end
toc
Elapsed time is 0.176980 seconds.
% Style 3
tic
for i=1:1e3
a=1:2:300000;
end
toc
Elapsed time is 0.184836 seconds.
  7 Comments
Chunru
Chunru on 20 Jul 2022
@Bruno Luong Thanks for the info. To me, it is a bit of style and a bit of habit (good or bad) to use square brackets sometimes. I actually lost the track where I got the habit :-(. There was no code analyser for very long time since I started to use MATLAB. Many "bad" habits may be picked up along the way.
Perhaps I should also blame MATLAB for that trait of possibly bad habit (just kidding, I am not blaming MATLAB for this :-))

Sign in to comment.

Answers (4)

Bruno Luong
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
a = 1×7
1 2 3 4 5 6 7
b = [1 : 5 +2]
b = 1×6
1 2 3 4 5 2
c = (1 : 5 +2)
c = 1×7
1 2 3 4 5 6 7
  1 Comment
Chunru
Chunru on 16 Jul 2022
Good points.
However, I think the reason is still not strong enough for discouraging use of [ ] when the style is concerned.

Sign in to comment.


Steven Lord
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
Chunru
Chunru on 17 Jul 2022
I appreciate your effort on mitigating the situation. I have no intention to escalate the things and I just want to have a peace of mind. I am not sure if you have followed through the conversations and I am ready to appologize if I have been on the wrong side.
"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'."
Talking about frequent contributors, I am amazed with many of them and adimired them as heros. I may be wrong to say "it has nothing to do with how frequently one contribute". A second thought is that the more one contributes, the bigger responsibilities one should have. Perhaps there should be an even higher standard to be expected.
I thank you again for your effort. All I am trying to advocate is nice place for each of us.
Many thanks.

Sign in to comment.


Walter Roberson
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
Bruno Luong on 16 Jul 2022
Edited: Bruno Luong on 16 Jul 2022
Really?
foo
Warning: MATLAB debugger can only stop in MATLAB code files, and "libmwm_interpreter>horzcat" is not a MATLAB code file.
Instead, the debugger will stop at the point right before "libmwm_interpreter>horzcat" is called.
ans = 1×10
4.1416 5.1416 6.1416 7.1416 8.1416 9.1416 10.1416 11.1416 12.1416 13.1416
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.

Sign in to comment.


Walter Roberson
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)
ans = 1.7918e-04
ans = 1.5834e-06
mean(t2), std(t2)
ans = 1.8140e-04
ans = 5.5645e-06
function without()
data = 1:2:300000;
end
function with()
data = [1:2:300000];
end
  10 Comments
Walter Roberson
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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!