Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Don't call a constant using <code>myClass.myconst</code> without creating an instance first! Unless speed is not an issue. I was under the impression that the first call to a constant property would create an instance and then all future calls would reference that instance, (<a href="http://www.mathworks.com/help/techdoc/matlab_oop/bso_zpq-1.html" rel="nofollow">Properties with Constant Values</a>), but I no longer believe that to be the case. I created a very basic test function of the form:</p> <pre><code>tic; for n = 1:N a = myObj.field; end t = toc; </code></pre> <p>With classes defined like:</p> <pre><code>classdef TestObj properties field = 10; end end </code></pre> <p>or:</p> <pre><code>classdef TestHandleObj &lt; handle properties field = 10; end end </code></pre> <p>or: </p> <pre><code>classdef TestConstant properties (Constant) field = 10; end end </code></pre> <p>For different cases of objects, handle-objects, nested objects etc (as well as assignment operations). Note that these were all scalars; I didn't investigate arrays, cells or chars. For N = 1,000,000 my results (for total elapsed time) were:</p> <pre><code>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 &gt; methods' //This calls get and set methods that loop internally 0.1574 0.3053 'TestHandleObj &gt; methods' //get and set methods (internal loop) </code></pre> <p>I also created a Java class and ran a similar test:</p> <pre><code> 12.18 17.53 'jObj.field &gt; in matlab for loop' 0.0043 0.0039 'jObj.get and jObj.set loop N times internally' </code></pre> <p>The overhead in calling the Java object is high, but within the object, simple access and assign operations happen as fast as regular matlab objects. If you want reference behavior to boot, Java may be the way to go. I did not investigate object calls within nested functions, but I've seen some weird things. Also, the profiler is garbage when it comes to a lot of this stuff, which is why I switched to manually saving the times.</p> <p>For reference, the Java class used:</p> <pre><code>public class JtestObj { public double field = 10; public double getMe() { double N = 1000000; double val = 0; for (int i = 1; i &lt; N; i++) { val = this.field; } return val; } public void setMe(double val) { double N = 1000000; for (int i = 1; i &lt; N; i++){ this.field = val; } } } </code></pre> <p>On a related note, here's a link to a table of NIST constants: <a href="http://physics.nist.gov/cuu/Constants/Table/allascii.txt" rel="nofollow">ascii table</a> and a matlab function that returns a struct with those listed values: <a href="http://www.mathworks.com/matlabcentral/fileexchange/27031-fundamental-physical-constants/content/NISTconstants.m" rel="nofollow">Matlab FileExchange</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload