performance - Perform a collection of commands as fast as possible in Java -
In my project I want to load a large amount of functions from the script into the archive and want to execute them later ( Maybe many times) without reading the script again. I need access to the works as soon as possible and thought of two ways to do this:
-
Command pattern: My first idea is to store script commands as objects (Command pattern) inside the archive and all of them in each-loop when I need to execute the script. Although the code would be easy to read, I think that using all the different commands through the objects will cost a lot of performance and memory. (Command command: command list) {command.execute (); Collection of letters and switch episodes: My second idea was to store primitive variables in the archive such as four or one collection and one switch-case -Each loop. I will use primitives because I think its intensity, that is, string objects, so I will use a library like TRAV. I think that in this way it can be faster than the command pattern because there is no command to reach the object. In addition, less memory will be captured, on the other hand, I feel that it can be slow because command pattern can directly use the right function, whereas switch-case construction has to be checked several times if four, A, B, C, D and so on.
For
(four commands: command list) {switch (command) {case 'a': doA (); break; Case 'B': DOB (); break; Case 'C': ...}}
Do you think a path is better? Do you know in other ways? What kind of collection would you recommend?
Boil your question below: polymorphis vs switch statement.
The former is better customary and readable because whenever someone wants to know what is going on or to be added to it, then using a large number of keys (or your case) Characters in) do not need to be used.
In terms of performance, either to be very negligible and highly customized, because they are very common and simple, thus it is better to worry about readability / maintenance compared to optimization and for this, polymorphism is better .
Comments
Post a Comment