Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- classdef MyConstants
- properties (Constant = true)
- SECONDS_PER_HOUR = 60*60;
- DISTANCE_TO_MOON_KM = 384403;
- end
- end
- >> disp(MyConstants.SECONDS_PER_HOUR)
- 3600
- >> MyConstants.SECONDS_PER_HOUR
- ans =
- 3600
- >> MyConstants.SECONDS_PER_HOUR = 42
- MyConstants =
- SECONDS_PER_HOUR: 42
- >> whos
- Name Size Bytes Class Attributes
- MyConstants 1x1 132 struct
- ans 1x1 8 double
- function broken_constant_use
- MyConstants(); % "import" to protect assignment
- MyConstants.SECONDS_PER_HOUR = 42 % this bug is a syntax error now
- tic;
- for n = 1:N
- a = myObj.field;
- end
- t = toc;
- classdef TestObj
- properties
- field = 10;
- end
- end
- classdef TestHandleObj < handle
- properties
- field = 10;
- end
- end
- classdef TestConstant
- properties (Constant)
- field = 10;
- end
- end
- Access(s) Assign(s) Type of object/call
- 0.0034 0.0042 'myObj.field'
- 0.0033 0.0042 'myStruct.field'
- 0.0034 0.0033 'myVar' //Plain old workspace evaluation
- 0.0033 0.0042 'myNestedObj.obj.field'
- 0.1581 0.3066 'myHandleObj.field'
- 0.1694 0.3124 'myNestedHandleObj.handleObj.field'
- 29.2161 - 'TestConstant.const' //Call directly to class(supposed to be faster)
- 0.0034 - 'myTestConstant.const' //Create an instance of TestConstant
- 0.0051 0.0078 'TestObj > methods' //This calls get and set methods that loop internally
- 0.1574 0.3053 'TestHandleObj > methods' //get and set methods (internal loop)
- 12.18 17.53 'jObj.field > in matlab for loop'
- 0.0043 0.0039 'jObj.get and jObj.set loop N times internally'
- public class JtestObj {
- public double field = 10;
- public double getMe() {
- double N = 1000000;
- double val = 0;
- for (int i = 1; i < N; i++) {
- val = this.field;
- }
- return val;
- }
- public void setMe(double val) {
- double N = 1000000;
- for (int i = 1; i < N; i++){
- this.field = val;
- }
- }
- }
- % Define constants
- params.PI = 3.1416;
- params.SQRT2 = 1.414;
- % Call a function which needs one or more of the constants
- myFunction( params );
- params = getConstants();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement