Performance of MATLAB's class constants
I have several helper functions in my code which get called many times for a given numerical calculation. Those helper functions use some constant values for their calculations. The same constant value might be used by more than one helper function.
This seems like an ideal scenario to define class properties with constant values. However, I have done some benchmark tests and I am quite surprised with the results.
Consider the following class for example (Consts.m
):
classdef Consts
properties (Constant)
A = 0.5
B = 3
end
properties
VariableA
VariableB
end
methods
function obj = Consts()
obj.VariableA = 0.5;
obj.VariableB = 3;
end
end
end
And the following file (speed_tests.m
):
function speed_tests()
tic;
for i = 1:200000
direct_constant_access(1, 2);
end
fprintf('Direct constant access: ');
toc;
tic;
c = Consts();
for i = 1:200000
passing_extra_obj(1, 2, c);
end
fprintf('Passing extra object: ');
toc;
tic;
for i = 1:200000
persistent_constants(1, 2);
end
fprintf('Persistent constants: ');
toc;
% Let's assume this code is executed at some point externally:
% global A B;
% A = 0.5;
% B = 3;
tic;
for i = 1:200000
defined_globally(1, 2);
end
fprintf('Defined globally: ');
toc;
tic;
for i = 1:200000
hardcoded(1, 2);
end
fprintf('Hardcoded: ');
toc;
tic;
for i = 1:200000
hardcoded_v2(1, 2);
end
fprintf('Hardcoded v2: ');
toc;
end
function val = direct_constant_access(a, b)
val = (a + Consts.A)^2 + log(b * Consts.B);
end
function val = passing_extra_obj(a, b, obj)
val = (a + obj.VariableA)^2 + log(b * obj.VariableB);
end
function val = persistent_constants(a, b)
persistent A B;
if isempty(A)
A = Consts.A^2;
B = Consts.B;
end
val = (a + A)^2 + log(b * B);
end
function val = defined_globally(a, b)
global A B;
val = (a + A)^2 + log(b * B);
end
function val = hardcoded(a, b)
val = (a + 0.5)^2 + log(b * 3);
end
function val = hardcoded_v2(a, b)
A = 0.5;
B = 3;
val = (a + A)^2 + log(b * B);
end
When I run speed_tests()
on MATLAB R2010b, this is what I obtain (your mileage may vary):
>> speed_tests()
Direct constant access: Elapsed time is 5.973690 seconds.
Passing extra object: Elapsed time is 1.760897 seconds.
Persistent constants: Elapsed time is 1.594263 seconds.
Defined globally: Elapsed time is 1.559441 seconds.
Hardcoded: Elapsed time is 0.673995 seconds.
Hardcoded v2: Elapsed time is 0.661189 seconds.
Perhaps I am too used to other programming languages (where true constants might simply get replaced by literals at compile time), but is accessing class constants really that slow in MATLAB or am I missing something?
When I try the same in MATLAB R2013a (same computer), this direct constant access seems to have improved quite a lot:
>> speed_tests()
Direct constant access: Elapsed time is 2.168146 seconds.
Passing extra object: Elapsed time is 1.593721 seconds.
Persistent constants: Elapsed time is 2.302785 seconds.
Defined globally: Elapsed time is 1.404252 seconds.
Hardcoded: Elapsed time is 0.531191 seconds.
Hardcoded v2: Elapsed time is 0.493668 seconds.
Still, none of the non-hardcoded versions is anywhere near the hardcoded ones. These are the only two MATLAB versions I have available at work, so I am unaware if this has kept improving over the years (and it wouldn't be relevant for myself, since I wouldn't be able to use newer versions anyway).
The CPU time is quite an important factor for what I am developing, but I would like to avoid filling the code with hardcoded literals if I can. Aren't class constants the alleged way of avoiding this?
Is there anything else that I could consider instead?
Note: the real helper functions get called with different arguments each time, so caching the result does not help in my case.
matlab class-constants
add a comment |
I have several helper functions in my code which get called many times for a given numerical calculation. Those helper functions use some constant values for their calculations. The same constant value might be used by more than one helper function.
This seems like an ideal scenario to define class properties with constant values. However, I have done some benchmark tests and I am quite surprised with the results.
Consider the following class for example (Consts.m
):
classdef Consts
properties (Constant)
A = 0.5
B = 3
end
properties
VariableA
VariableB
end
methods
function obj = Consts()
obj.VariableA = 0.5;
obj.VariableB = 3;
end
end
end
And the following file (speed_tests.m
):
function speed_tests()
tic;
for i = 1:200000
direct_constant_access(1, 2);
end
fprintf('Direct constant access: ');
toc;
tic;
c = Consts();
for i = 1:200000
passing_extra_obj(1, 2, c);
end
fprintf('Passing extra object: ');
toc;
tic;
for i = 1:200000
persistent_constants(1, 2);
end
fprintf('Persistent constants: ');
toc;
% Let's assume this code is executed at some point externally:
% global A B;
% A = 0.5;
% B = 3;
tic;
for i = 1:200000
defined_globally(1, 2);
end
fprintf('Defined globally: ');
toc;
tic;
for i = 1:200000
hardcoded(1, 2);
end
fprintf('Hardcoded: ');
toc;
tic;
for i = 1:200000
hardcoded_v2(1, 2);
end
fprintf('Hardcoded v2: ');
toc;
end
function val = direct_constant_access(a, b)
val = (a + Consts.A)^2 + log(b * Consts.B);
end
function val = passing_extra_obj(a, b, obj)
val = (a + obj.VariableA)^2 + log(b * obj.VariableB);
end
function val = persistent_constants(a, b)
persistent A B;
if isempty(A)
A = Consts.A^2;
B = Consts.B;
end
val = (a + A)^2 + log(b * B);
end
function val = defined_globally(a, b)
global A B;
val = (a + A)^2 + log(b * B);
end
function val = hardcoded(a, b)
val = (a + 0.5)^2 + log(b * 3);
end
function val = hardcoded_v2(a, b)
A = 0.5;
B = 3;
val = (a + A)^2 + log(b * B);
end
When I run speed_tests()
on MATLAB R2010b, this is what I obtain (your mileage may vary):
>> speed_tests()
Direct constant access: Elapsed time is 5.973690 seconds.
Passing extra object: Elapsed time is 1.760897 seconds.
Persistent constants: Elapsed time is 1.594263 seconds.
Defined globally: Elapsed time is 1.559441 seconds.
Hardcoded: Elapsed time is 0.673995 seconds.
Hardcoded v2: Elapsed time is 0.661189 seconds.
Perhaps I am too used to other programming languages (where true constants might simply get replaced by literals at compile time), but is accessing class constants really that slow in MATLAB or am I missing something?
When I try the same in MATLAB R2013a (same computer), this direct constant access seems to have improved quite a lot:
>> speed_tests()
Direct constant access: Elapsed time is 2.168146 seconds.
Passing extra object: Elapsed time is 1.593721 seconds.
Persistent constants: Elapsed time is 2.302785 seconds.
Defined globally: Elapsed time is 1.404252 seconds.
Hardcoded: Elapsed time is 0.531191 seconds.
Hardcoded v2: Elapsed time is 0.493668 seconds.
Still, none of the non-hardcoded versions is anywhere near the hardcoded ones. These are the only two MATLAB versions I have available at work, so I am unaware if this has kept improving over the years (and it wouldn't be relevant for myself, since I wouldn't be able to use newer versions anyway).
The CPU time is quite an important factor for what I am developing, but I would like to avoid filling the code with hardcoded literals if I can. Aren't class constants the alleged way of avoiding this?
Is there anything else that I could consider instead?
Note: the real helper functions get called with different arguments each time, so caching the result does not help in my case.
matlab class-constants
add a comment |
I have several helper functions in my code which get called many times for a given numerical calculation. Those helper functions use some constant values for their calculations. The same constant value might be used by more than one helper function.
This seems like an ideal scenario to define class properties with constant values. However, I have done some benchmark tests and I am quite surprised with the results.
Consider the following class for example (Consts.m
):
classdef Consts
properties (Constant)
A = 0.5
B = 3
end
properties
VariableA
VariableB
end
methods
function obj = Consts()
obj.VariableA = 0.5;
obj.VariableB = 3;
end
end
end
And the following file (speed_tests.m
):
function speed_tests()
tic;
for i = 1:200000
direct_constant_access(1, 2);
end
fprintf('Direct constant access: ');
toc;
tic;
c = Consts();
for i = 1:200000
passing_extra_obj(1, 2, c);
end
fprintf('Passing extra object: ');
toc;
tic;
for i = 1:200000
persistent_constants(1, 2);
end
fprintf('Persistent constants: ');
toc;
% Let's assume this code is executed at some point externally:
% global A B;
% A = 0.5;
% B = 3;
tic;
for i = 1:200000
defined_globally(1, 2);
end
fprintf('Defined globally: ');
toc;
tic;
for i = 1:200000
hardcoded(1, 2);
end
fprintf('Hardcoded: ');
toc;
tic;
for i = 1:200000
hardcoded_v2(1, 2);
end
fprintf('Hardcoded v2: ');
toc;
end
function val = direct_constant_access(a, b)
val = (a + Consts.A)^2 + log(b * Consts.B);
end
function val = passing_extra_obj(a, b, obj)
val = (a + obj.VariableA)^2 + log(b * obj.VariableB);
end
function val = persistent_constants(a, b)
persistent A B;
if isempty(A)
A = Consts.A^2;
B = Consts.B;
end
val = (a + A)^2 + log(b * B);
end
function val = defined_globally(a, b)
global A B;
val = (a + A)^2 + log(b * B);
end
function val = hardcoded(a, b)
val = (a + 0.5)^2 + log(b * 3);
end
function val = hardcoded_v2(a, b)
A = 0.5;
B = 3;
val = (a + A)^2 + log(b * B);
end
When I run speed_tests()
on MATLAB R2010b, this is what I obtain (your mileage may vary):
>> speed_tests()
Direct constant access: Elapsed time is 5.973690 seconds.
Passing extra object: Elapsed time is 1.760897 seconds.
Persistent constants: Elapsed time is 1.594263 seconds.
Defined globally: Elapsed time is 1.559441 seconds.
Hardcoded: Elapsed time is 0.673995 seconds.
Hardcoded v2: Elapsed time is 0.661189 seconds.
Perhaps I am too used to other programming languages (where true constants might simply get replaced by literals at compile time), but is accessing class constants really that slow in MATLAB or am I missing something?
When I try the same in MATLAB R2013a (same computer), this direct constant access seems to have improved quite a lot:
>> speed_tests()
Direct constant access: Elapsed time is 2.168146 seconds.
Passing extra object: Elapsed time is 1.593721 seconds.
Persistent constants: Elapsed time is 2.302785 seconds.
Defined globally: Elapsed time is 1.404252 seconds.
Hardcoded: Elapsed time is 0.531191 seconds.
Hardcoded v2: Elapsed time is 0.493668 seconds.
Still, none of the non-hardcoded versions is anywhere near the hardcoded ones. These are the only two MATLAB versions I have available at work, so I am unaware if this has kept improving over the years (and it wouldn't be relevant for myself, since I wouldn't be able to use newer versions anyway).
The CPU time is quite an important factor for what I am developing, but I would like to avoid filling the code with hardcoded literals if I can. Aren't class constants the alleged way of avoiding this?
Is there anything else that I could consider instead?
Note: the real helper functions get called with different arguments each time, so caching the result does not help in my case.
matlab class-constants
I have several helper functions in my code which get called many times for a given numerical calculation. Those helper functions use some constant values for their calculations. The same constant value might be used by more than one helper function.
This seems like an ideal scenario to define class properties with constant values. However, I have done some benchmark tests and I am quite surprised with the results.
Consider the following class for example (Consts.m
):
classdef Consts
properties (Constant)
A = 0.5
B = 3
end
properties
VariableA
VariableB
end
methods
function obj = Consts()
obj.VariableA = 0.5;
obj.VariableB = 3;
end
end
end
And the following file (speed_tests.m
):
function speed_tests()
tic;
for i = 1:200000
direct_constant_access(1, 2);
end
fprintf('Direct constant access: ');
toc;
tic;
c = Consts();
for i = 1:200000
passing_extra_obj(1, 2, c);
end
fprintf('Passing extra object: ');
toc;
tic;
for i = 1:200000
persistent_constants(1, 2);
end
fprintf('Persistent constants: ');
toc;
% Let's assume this code is executed at some point externally:
% global A B;
% A = 0.5;
% B = 3;
tic;
for i = 1:200000
defined_globally(1, 2);
end
fprintf('Defined globally: ');
toc;
tic;
for i = 1:200000
hardcoded(1, 2);
end
fprintf('Hardcoded: ');
toc;
tic;
for i = 1:200000
hardcoded_v2(1, 2);
end
fprintf('Hardcoded v2: ');
toc;
end
function val = direct_constant_access(a, b)
val = (a + Consts.A)^2 + log(b * Consts.B);
end
function val = passing_extra_obj(a, b, obj)
val = (a + obj.VariableA)^2 + log(b * obj.VariableB);
end
function val = persistent_constants(a, b)
persistent A B;
if isempty(A)
A = Consts.A^2;
B = Consts.B;
end
val = (a + A)^2 + log(b * B);
end
function val = defined_globally(a, b)
global A B;
val = (a + A)^2 + log(b * B);
end
function val = hardcoded(a, b)
val = (a + 0.5)^2 + log(b * 3);
end
function val = hardcoded_v2(a, b)
A = 0.5;
B = 3;
val = (a + A)^2 + log(b * B);
end
When I run speed_tests()
on MATLAB R2010b, this is what I obtain (your mileage may vary):
>> speed_tests()
Direct constant access: Elapsed time is 5.973690 seconds.
Passing extra object: Elapsed time is 1.760897 seconds.
Persistent constants: Elapsed time is 1.594263 seconds.
Defined globally: Elapsed time is 1.559441 seconds.
Hardcoded: Elapsed time is 0.673995 seconds.
Hardcoded v2: Elapsed time is 0.661189 seconds.
Perhaps I am too used to other programming languages (where true constants might simply get replaced by literals at compile time), but is accessing class constants really that slow in MATLAB or am I missing something?
When I try the same in MATLAB R2013a (same computer), this direct constant access seems to have improved quite a lot:
>> speed_tests()
Direct constant access: Elapsed time is 2.168146 seconds.
Passing extra object: Elapsed time is 1.593721 seconds.
Persistent constants: Elapsed time is 2.302785 seconds.
Defined globally: Elapsed time is 1.404252 seconds.
Hardcoded: Elapsed time is 0.531191 seconds.
Hardcoded v2: Elapsed time is 0.493668 seconds.
Still, none of the non-hardcoded versions is anywhere near the hardcoded ones. These are the only two MATLAB versions I have available at work, so I am unaware if this has kept improving over the years (and it wouldn't be relevant for myself, since I wouldn't be able to use newer versions anyway).
The CPU time is quite an important factor for what I am developing, but I would like to avoid filling the code with hardcoded literals if I can. Aren't class constants the alleged way of avoiding this?
Is there anything else that I could consider instead?
Note: the real helper functions get called with different arguments each time, so caching the result does not help in my case.
matlab class-constants
matlab class-constants
asked Nov 21 '18 at 15:43
FernAndrFernAndr
406314
406314
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I've run into this issue as well, if there is a trick to reducing the overhead of accessing class objects I would also love to know.
When I can I try to minimize the number of times the object is accessed. In your example I would access A and B once before starting the loop, and then pass them as arguments to each function call.
function speed_tests()
tic;
A = Consts.A;
B = Consts.B;
for i = 1:200000
passing_arguments(1, 2, A, B);
end
fprintf('Passing arguments: ');
toc;
tic;
for i = 1:200000
persistent_constants(1, 2);
end
fprintf('Persistent constants: ');
toc;
tic;
for i = 1:200000
hardcoded(1, 2);
end
fprintf('Hardcoded: ');
toc;
end
function val = passing_arguments(a, b, A, B)
val = (a + A)^2 + log(b * B);
end
function val = persistent_constants(a, b)
persistent A B;
if isempty(A)
A = Consts.A^2;
B = Consts.B;
end
val = (a + A)^2 + log(b * B);
end
function val = hardcoded(a, b)
val = (a + 0.5)^2 + log(b * 3);
end
Output:
Passing arguments: Elapsed time is 0.035402 seconds.
Persistent constants: Elapsed time is 0.208998 seconds.
Hardcoded: Elapsed time is 0.027781 seconds.
Yeah, I think I will opt for either this or hardcoding them, depending on the situation. Many of these constants are just for unit conversion, so a line likeval = a * 0.45; % from lb to kg
is still very readable. These constants should never ever change either, so having to replace these hardcoded values manually by something else is not an issue either. For anything more complicated, I guess I will pass them as arguments. Still, a bit funny that this would be much faster than the class constants, I wouldn't have expected that
– FernAndr
Nov 21 '18 at 19:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415665%2fperformance-of-matlabs-class-constants%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've run into this issue as well, if there is a trick to reducing the overhead of accessing class objects I would also love to know.
When I can I try to minimize the number of times the object is accessed. In your example I would access A and B once before starting the loop, and then pass them as arguments to each function call.
function speed_tests()
tic;
A = Consts.A;
B = Consts.B;
for i = 1:200000
passing_arguments(1, 2, A, B);
end
fprintf('Passing arguments: ');
toc;
tic;
for i = 1:200000
persistent_constants(1, 2);
end
fprintf('Persistent constants: ');
toc;
tic;
for i = 1:200000
hardcoded(1, 2);
end
fprintf('Hardcoded: ');
toc;
end
function val = passing_arguments(a, b, A, B)
val = (a + A)^2 + log(b * B);
end
function val = persistent_constants(a, b)
persistent A B;
if isempty(A)
A = Consts.A^2;
B = Consts.B;
end
val = (a + A)^2 + log(b * B);
end
function val = hardcoded(a, b)
val = (a + 0.5)^2 + log(b * 3);
end
Output:
Passing arguments: Elapsed time is 0.035402 seconds.
Persistent constants: Elapsed time is 0.208998 seconds.
Hardcoded: Elapsed time is 0.027781 seconds.
Yeah, I think I will opt for either this or hardcoding them, depending on the situation. Many of these constants are just for unit conversion, so a line likeval = a * 0.45; % from lb to kg
is still very readable. These constants should never ever change either, so having to replace these hardcoded values manually by something else is not an issue either. For anything more complicated, I guess I will pass them as arguments. Still, a bit funny that this would be much faster than the class constants, I wouldn't have expected that
– FernAndr
Nov 21 '18 at 19:03
add a comment |
I've run into this issue as well, if there is a trick to reducing the overhead of accessing class objects I would also love to know.
When I can I try to minimize the number of times the object is accessed. In your example I would access A and B once before starting the loop, and then pass them as arguments to each function call.
function speed_tests()
tic;
A = Consts.A;
B = Consts.B;
for i = 1:200000
passing_arguments(1, 2, A, B);
end
fprintf('Passing arguments: ');
toc;
tic;
for i = 1:200000
persistent_constants(1, 2);
end
fprintf('Persistent constants: ');
toc;
tic;
for i = 1:200000
hardcoded(1, 2);
end
fprintf('Hardcoded: ');
toc;
end
function val = passing_arguments(a, b, A, B)
val = (a + A)^2 + log(b * B);
end
function val = persistent_constants(a, b)
persistent A B;
if isempty(A)
A = Consts.A^2;
B = Consts.B;
end
val = (a + A)^2 + log(b * B);
end
function val = hardcoded(a, b)
val = (a + 0.5)^2 + log(b * 3);
end
Output:
Passing arguments: Elapsed time is 0.035402 seconds.
Persistent constants: Elapsed time is 0.208998 seconds.
Hardcoded: Elapsed time is 0.027781 seconds.
Yeah, I think I will opt for either this or hardcoding them, depending on the situation. Many of these constants are just for unit conversion, so a line likeval = a * 0.45; % from lb to kg
is still very readable. These constants should never ever change either, so having to replace these hardcoded values manually by something else is not an issue either. For anything more complicated, I guess I will pass them as arguments. Still, a bit funny that this would be much faster than the class constants, I wouldn't have expected that
– FernAndr
Nov 21 '18 at 19:03
add a comment |
I've run into this issue as well, if there is a trick to reducing the overhead of accessing class objects I would also love to know.
When I can I try to minimize the number of times the object is accessed. In your example I would access A and B once before starting the loop, and then pass them as arguments to each function call.
function speed_tests()
tic;
A = Consts.A;
B = Consts.B;
for i = 1:200000
passing_arguments(1, 2, A, B);
end
fprintf('Passing arguments: ');
toc;
tic;
for i = 1:200000
persistent_constants(1, 2);
end
fprintf('Persistent constants: ');
toc;
tic;
for i = 1:200000
hardcoded(1, 2);
end
fprintf('Hardcoded: ');
toc;
end
function val = passing_arguments(a, b, A, B)
val = (a + A)^2 + log(b * B);
end
function val = persistent_constants(a, b)
persistent A B;
if isempty(A)
A = Consts.A^2;
B = Consts.B;
end
val = (a + A)^2 + log(b * B);
end
function val = hardcoded(a, b)
val = (a + 0.5)^2 + log(b * 3);
end
Output:
Passing arguments: Elapsed time is 0.035402 seconds.
Persistent constants: Elapsed time is 0.208998 seconds.
Hardcoded: Elapsed time is 0.027781 seconds.
I've run into this issue as well, if there is a trick to reducing the overhead of accessing class objects I would also love to know.
When I can I try to minimize the number of times the object is accessed. In your example I would access A and B once before starting the loop, and then pass them as arguments to each function call.
function speed_tests()
tic;
A = Consts.A;
B = Consts.B;
for i = 1:200000
passing_arguments(1, 2, A, B);
end
fprintf('Passing arguments: ');
toc;
tic;
for i = 1:200000
persistent_constants(1, 2);
end
fprintf('Persistent constants: ');
toc;
tic;
for i = 1:200000
hardcoded(1, 2);
end
fprintf('Hardcoded: ');
toc;
end
function val = passing_arguments(a, b, A, B)
val = (a + A)^2 + log(b * B);
end
function val = persistent_constants(a, b)
persistent A B;
if isempty(A)
A = Consts.A^2;
B = Consts.B;
end
val = (a + A)^2 + log(b * B);
end
function val = hardcoded(a, b)
val = (a + 0.5)^2 + log(b * 3);
end
Output:
Passing arguments: Elapsed time is 0.035402 seconds.
Persistent constants: Elapsed time is 0.208998 seconds.
Hardcoded: Elapsed time is 0.027781 seconds.
answered Nov 21 '18 at 16:52
MattMatt
1,157112
1,157112
Yeah, I think I will opt for either this or hardcoding them, depending on the situation. Many of these constants are just for unit conversion, so a line likeval = a * 0.45; % from lb to kg
is still very readable. These constants should never ever change either, so having to replace these hardcoded values manually by something else is not an issue either. For anything more complicated, I guess I will pass them as arguments. Still, a bit funny that this would be much faster than the class constants, I wouldn't have expected that
– FernAndr
Nov 21 '18 at 19:03
add a comment |
Yeah, I think I will opt for either this or hardcoding them, depending on the situation. Many of these constants are just for unit conversion, so a line likeval = a * 0.45; % from lb to kg
is still very readable. These constants should never ever change either, so having to replace these hardcoded values manually by something else is not an issue either. For anything more complicated, I guess I will pass them as arguments. Still, a bit funny that this would be much faster than the class constants, I wouldn't have expected that
– FernAndr
Nov 21 '18 at 19:03
Yeah, I think I will opt for either this or hardcoding them, depending on the situation. Many of these constants are just for unit conversion, so a line like
val = a * 0.45; % from lb to kg
is still very readable. These constants should never ever change either, so having to replace these hardcoded values manually by something else is not an issue either. For anything more complicated, I guess I will pass them as arguments. Still, a bit funny that this would be much faster than the class constants, I wouldn't have expected that– FernAndr
Nov 21 '18 at 19:03
Yeah, I think I will opt for either this or hardcoding them, depending on the situation. Many of these constants are just for unit conversion, so a line like
val = a * 0.45; % from lb to kg
is still very readable. These constants should never ever change either, so having to replace these hardcoded values manually by something else is not an issue either. For anything more complicated, I guess I will pass them as arguments. Still, a bit funny that this would be much faster than the class constants, I wouldn't have expected that– FernAndr
Nov 21 '18 at 19:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415665%2fperformance-of-matlabs-class-constants%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown