Const (probably revisited)
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
In professional programming languages like C or C++, you can define constants NOT AS PARAMETERS, but as compiler directives. These constants work like "replace all occurrences of "PI" by 3.14 and then start the program". This means that you can define miriades of constants, making your code much more transparant and robust, without any consequences to the execution speed. During execution, there are no parameters created, no memory allocation, no parameters passed, no m-files parsed, but all constants have been replaced by values prior to execution.
That is what a programming language should look like. Using classes with constants is hopelessly complicated, affects your execution speed and requires memory allocation for constants during execution. That is plain silly.
MATLAB should implement constants in the professional sense. It can't be too hard to do: a const clause for primitives variables alone (double, unit32, char, etc.) would already be a great help to programming matlab. Calling an m-file with const would then not mean to create these variables, but to replace all "const variable" names by the const value during the parsing of the code. Why is this not available?
Personally I would not mind, to avoid collision with non-const variables, to state that const variable names should start with a "0" or a "#" or a "!". No non-const variable may have that, so no collisions.
1 Kommentar
Stephen23
am 12 Apr. 2019
"In professional programming languages..."
I work with MATLAB (and other languages) professionally. I know others who do too. Does that make MATLAB a "professional programming language", or are you using some special definition that conveniently excludes anyone who actually works professionally with MATLAB?
"...without any consequences to the execution speed"
Just pass parameters as variables.
I very much doubt that passing variables is a significant bottleneck in your MATLAB code.
"During execution, there are no parameters created, no memory allocation, no parameters passed, no m-files parsed,"
What you describe is a precompiled, statically-typed language with compile-time memory allocation. MATLAB is a dynamically-typed high-level interpreted language which uses JIT compilation (and it is not the only one around). You appear to be arguing that interpreted languages should not exist: lets burn them!
"Using classes with constants is hopelessly complicated,...That is plain silly."
I agree: simply define them once and pass the parameters in an array (numeric, cell, structure, etc): this is much simpler and does not cause any duplication of data in memory.
Antworten (3)
Walter Roberson
am 12 Apr. 2019
It has nothing to do with "professional". What you are talking about is compile time constants, but MATLAB is not a compiler.
It is not known that MATLAB uses memoryless constants even for literals. F(x)+1 is defined as plus(F(x), 1) with the correct version of plus to call not known until type resolution is done on the return result of F(x). It could be a user defined function; it could be mex call; it could be a built-in; it could be Mathworks .m or .p or .slx. For user functions and mex calls and m and p files, literals need to be assigned memory. For slx a literal might hypothetically not need memory but that would depend on the exact blocks needed. So much analysis and predictions would be needed with regard to slx that it would be much more efficient to just always allocate memory for the literal at the boundary.
Meanwhile in order for built-in to be able to avoid allocating memory for literals, there would have to be 2^N version implemented, where N is the number of parameters....
0 Kommentare
Jasper van Casteren
am 12 Apr. 2019
2 Kommentare
Walter Roberson
am 12 Apr. 2019
Bearbeitet: Walter Roberson
am 12 Apr. 2019
Your posts require that memory not be allocated for the constants. Consider the expression A+SPEED where SPEED is one of the constants. In a setup where memory is allocated for literals and constants then this is implemented by one built-in method per fundamental type, such as @uint8/plus, that accepts two arguments as allocated memory and adds them. But in your proposal it would not be permitted to allocate memory for SPEED so there would need to be four methods instead: @uint8/plus(memory, memory), @uint8/plus(memory, constant), @uint8/plus(constant, memory), @uint8/plus(constant, constant).
Remember that in MATLAB, indexing is a function call, subsref() or subsasgn(). Those functions are passed cell arrays of indices because even for numeric arrays the bare colon operator is passed as ':' (and logicals and characters are valid indices). The No Memory requirement would require a fundamental change to indexing because currently all of the indices have allocated memory but suddenly now that would not be permitted for the "professional" constants.
MyArray(53) is currently handled by allocating memory for an unnamed numeric variable withddescriptor "type double sized 1, 1, local, not complex" and passing that location into the appropriate place in a cell in subsref. The *only* difference compared to a named variable SPEED=53; MyArray(SPEED) is lack of a symbol table entry "SPEED" -> descriptor. MyArray(literal) is not processed differently than MyArray(variable) other than there being a name associated with the descriptor. Your proposal would require treating your professional constants differently, not being permitted to construct the memory with descriptor node for them.
You might be thinking "Okay, when the professional constant is used then the value associated with the constant can be inserted into an appropriate descriptor and passed just as if a literal had been written." But then you have allocated memory to hold it, and if you ever refer to that constant twice then it would be more memory efficient to allocate memory only once and put it in the symbol table... as is done right now.
I can hear the "Oh, but optimization!" in the background. And... It turns out that MATLAB has a built in optimization layer called JIT that keeps track of locations and knows not to bother searching in the symbol table again provided that the variable was not cleared...
Stephen23
am 12 Apr. 2019
Bearbeitet: Stephen23
am 12 Apr. 2019
"And that can also be done with a interpreter."
Sure, and MATLAB does just that: simply pass your parameters as variable/s and MATLAB's JIT engine does not make copies of them (as long as you do not change them inside other workspaces). The simple MATLAB solution would be to use one single structure and pass that:
"Any suggestions?"
Stop trying to write MATLAB code as if it was C++.
Jasper van Casteren
am 15 Apr. 2019
1 Kommentar
Walter Roberson
am 15 Apr. 2019
Perhaps I have missed something but it looks to me as if you are reinventing databases, or tables, or object methods. that is, you can have a method named SPEED that returns a particular column of the stored object. Or just a general function named SPEED that returns a particular column of its input under the assumption that all inputs have the same layout.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!