Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. classdef MyConstants
  2. properties (Constant = true)
  3. SECONDS_PER_HOUR = 60*60;
  4. DISTANCE_TO_MOON_KM = 384403;
  5. end
  6. end
  7.  
  8. >> disp(MyConstants.SECONDS_PER_HOUR)
  9. 3600
  10.  
  11. >> MyConstants.SECONDS_PER_HOUR
  12. ans =
  13. 3600
  14. >> MyConstants.SECONDS_PER_HOUR = 42
  15. MyConstants =
  16. SECONDS_PER_HOUR: 42
  17. >> whos
  18. Name Size Bytes Class Attributes
  19.  
  20. MyConstants 1x1 132 struct
  21. ans 1x1 8 double
  22.  
  23. function broken_constant_use
  24. MyConstants(); % "import" to protect assignment
  25. MyConstants.SECONDS_PER_HOUR = 42 % this bug is a syntax error now
  26.  
  27. tic;
  28. for n = 1:N
  29. a = myObj.field;
  30. end
  31. t = toc;
  32.  
  33. classdef TestObj
  34. properties
  35. field = 10;
  36. end
  37. end
  38.  
  39. classdef TestHandleObj < handle
  40. properties
  41. field = 10;
  42. end
  43. end
  44.  
  45. classdef TestConstant
  46. properties (Constant)
  47. field = 10;
  48. end
  49. end
  50.  
  51. Access(s) Assign(s) Type of object/call
  52. 0.0034 0.0042 'myObj.field'
  53. 0.0033 0.0042 'myStruct.field'
  54. 0.0034 0.0033 'myVar' //Plain old workspace evaluation
  55. 0.0033 0.0042 'myNestedObj.obj.field'
  56. 0.1581 0.3066 'myHandleObj.field'
  57. 0.1694 0.3124 'myNestedHandleObj.handleObj.field'
  58. 29.2161 - 'TestConstant.const' //Call directly to class(supposed to be faster)
  59. 0.0034 - 'myTestConstant.const' //Create an instance of TestConstant
  60. 0.0051 0.0078 'TestObj > methods' //This calls get and set methods that loop internally
  61. 0.1574 0.3053 'TestHandleObj > methods' //get and set methods (internal loop)
  62.  
  63. 12.18 17.53 'jObj.field > in matlab for loop'
  64. 0.0043 0.0039 'jObj.get and jObj.set loop N times internally'
  65.  
  66. public class JtestObj {
  67. public double field = 10;
  68.  
  69. public double getMe() {
  70. double N = 1000000;
  71. double val = 0;
  72. for (int i = 1; i < N; i++) {
  73. val = this.field;
  74. }
  75.  
  76. return val;
  77. }
  78.  
  79. public void setMe(double val) {
  80. double N = 1000000;
  81. for (int i = 1; i < N; i++){
  82. this.field = val;
  83. }
  84. }
  85. }
  86.  
  87. % Define constants
  88. params.PI = 3.1416;
  89. params.SQRT2 = 1.414;
  90.  
  91. % Call a function which needs one or more of the constants
  92. myFunction( params );
  93.  
  94. params = getConstants();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement