Constants in MATLAB

前端 未结 8 915
孤城傲影
孤城傲影 2020-12-04 21:44

I\'ve come into ownership of a bunch of MATLAB code and have noticed a bunch of \"magic numbers\" scattered about the code. Typically, I like to make those constants in lan

8条回答
  •  不知归路
    2020-12-04 22:05

    Don't call a constant using myClass.myconst 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, (Properties with Constant Values), but I no longer believe that to be the case. I created a very basic test function of the form:

    tic;
    for n = 1:N
        a = myObj.field;
    end
    t = toc;
    

    With classes defined like:

    classdef TestObj
        properties
            field = 10;
        end
    end
    

    or:

    classdef TestHandleObj < handle
        properties
            field = 10;
        end
    end
    

    or:

    classdef TestConstant
        properties (Constant)
            field = 10;
        end
    end
    

    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:

    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)
    

    I also created a Java class and ran a similar test:

     12.18     17.53      'jObj.field > in matlab for loop'
      0.0043    0.0039    'jObj.get and jObj.set loop N times internally'
    

    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.

    For reference, the Java class used:

    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;
            }
         }
      }
    

    On a related note, here's a link to a table of NIST constants: ascii table and a matlab function that returns a struct with those listed values: Matlab FileExchange

提交回复
热议问题