-2

in my project I want to load a large amount of functions from a script into a collection to hold them and perform them later (probably many times) without reading the script again. I need the Access to the functions to be as fast as possible and thought about two ways of doing so:

  1. Command Pattern: My first idea was to store the script commands as objects (command pattern) inside of a collection and perform all of them within a for-each-loop when I need to perform the script. Although the Code will be easier to read I think it will cost a lot of performance and memory to access all the different commands through objects.

    for (Command command : commandList) { command.execute();

  2. Collection of chars and Switch Case: My second Idea was to store primitive variables like char or int into a collection and put a switch-case construct inside a for-each-loop. I would use primitives because I think its faster then i.e. String objects. Therefore I would use a library like Trove. I think this way it could be faster than the command pattern because no command object has to be accessed. Moreover, less memory will be occupied. On the other hand i think it could be slower because the command pattern can access the right function directly while the switch-case construct has to check many times if the char is a,b,c,d and so on.

    for (char command : commandList) { switch(command){ case 'a': doA(); break; case 'b': doB(); break; case 'c': ... } }

Do you think one way is better? Do you know another way? Which type of collection would you recommend?

Matze
  • 1
  • 2
  • `Although the Code will be easier to read I think it will cost a lot of performance and memory to access all the different commands through objects.` - If you won't store a huge amount of commands, the performance impact will be negligible. – helpermethod Jul 25 '14 at 12:23
  • What is the problem? As it is, this question is **opinion based**. – Afzaal Ahmad Zeeshan Jul 25 '14 at 12:24
  • I dont think its opinion based. The question is which way has the better performance, if I have huge Scripts with many hundred commands plus a variety of ~hundred different command types. – Matze Jul 25 '14 at 12:26
  • 2
    If you want performance issues, then it'd better be *millions* of commands, and each command should better not spend anything more than 100 nanoseconds executing. Otherwise you are not optimizing anything with the narrow aspect of the whole picture you have brought up. – Marko Topolnik Jul 25 '14 at 12:27
  • Well, it will be millions of Commands because many thousands of those scipts(command collections) will be executed. – Matze Jul 25 '14 at 12:32
  • Command is way better, even if it's a little bit slower – Leo Jul 25 '14 at 12:43

2 Answers2

2

Your question boils down to: Polymorphism vs Switch statement.

The former is better maintainable and readable since there is no need to slug through a large switch statement using magic numbers as its keys (or chars in your case) whenever one wants to know what is happening or add to it.

In terms of performance either should be rather negligible and highly optimized since they are very common and simple cases, thus it is better to worry about readability/maintainability than optimisation and therefor Polymorphism is better.

Stratege
  • 141
  • 5
0

I think second aproach is better.

On the other hand i think it could be slower because the command pattern can access the right function directly while the switch-case construct has to check many times if the char is a,b,c,d and so on.

Compiler will take care and optimize it. If your script file size is not in GB, it doesn't matter.

bigGuy
  • 1,732
  • 1
  • 22
  • 37