Switch Case, Select Active, or If Else If?
Spire_Jeff
Formerly Caffeinated Programmer
Here are some results from my benchmark code for the various methods. Initial testing is being performed on switching based on a single character. If I have some time or inspiration, I will try the test with strings and also with plain integers. Here are the test parameters:
- 4 Different methods are being tested. They are Switch Case, Select Active, If Else If, and multiple If statements.
- The method is passed a data array filled with characters. The method then sets the indexed value of nInfo to an integer based on the character at the corresponding indexed position of the data array. The function uses a for loop to parse the entire data array.
- Each test timer is started immediately before the method is called, and ended immediately after the method returns.
- Each method is tested 4 ways. First is with a FUNCTION CALL using a data array filled only with 'A'. Second is with a FUNCTION CALL using a data array filled only with 'Z'. Third is with a FUNCTION CALL using a data array filled with random characters from 'A' to 'Z'. Last is a DEFINE_CALL called using the same random array from test 3.
This is what I think should logically happen:
- The difference between a DEFINE_FUNCTION and a DEFINE_CALL should be negligible. This is mainly because the function/call is only being called once for the test. (I will setup another test specifically to test this aspect after these tests finish)
- The first three test methods should be fastest when dealing with an array filled only with 'A', slowest with an array filled with only 'Z', and somewhere in between for the random characters.
- The fourth test (multiple If statements) Should be the same speed regardless of the array used.
And, here are the results:
It seems that Switch Case wins on a single character. Select Active and If Else If are almost identical. The big surprise to me is that multiple If statements are slower when the data coming to them is random. I thought for sure that each if() statement would need to be evaluated and since one of them is always evaluating to true, it should take the same amount of time for each run through regardless of the result.
The timing on the first three methods follows the patterns I was thinking they should.
If you would like a copy of the code to play with, let me know and I'll post it here.
I am going to check the differences between a function and a call real quick
Jeff
- 4 Different methods are being tested. They are Switch Case, Select Active, If Else If, and multiple If statements.
- The method is passed a data array filled with characters. The method then sets the indexed value of nInfo to an integer based on the character at the corresponding indexed position of the data array. The function uses a for loop to parse the entire data array.
- Each test timer is started immediately before the method is called, and ended immediately after the method returns.
- Each method is tested 4 ways. First is with a FUNCTION CALL using a data array filled only with 'A'. Second is with a FUNCTION CALL using a data array filled only with 'Z'. Third is with a FUNCTION CALL using a data array filled with random characters from 'A' to 'Z'. Last is a DEFINE_CALL called using the same random array from test 3.
This is what I think should logically happen:
- The difference between a DEFINE_FUNCTION and a DEFINE_CALL should be negligible. This is mainly because the function/call is only being called once for the test. (I will setup another test specifically to test this aspect after these tests finish)
- The first three test methods should be fastest when dealing with an array filled only with 'A', slowest with an array filled with only 'Z', and somewhere in between for the random characters.
- The fourth test (multiple If statements) Should be the same speed regardless of the array used.
And, here are the results:
Line 1 (18:33:27):: ********************************************************* Line 2 (18:33:27):: * TEST 1 REPORT: SwitchCase "A" Function Line 3 (18:33:27):: * Most recent 5 runs: Line 4 (18:33:27):: * 1: 2089ms Line 5 (18:33:27):: * 2: 2096ms Line 6 (18:33:27):: * 3: 2090ms Line 7 (18:33:27):: * 4: 2088ms Line 8 (18:33:27):: * 5: 2097ms Line 9 (18:33:27):: *---------------------------------------------------------- Line 10 (18:33:27):: * Average run time: 2091ms - over 6 tests Line 11 (18:33:27):: ********************************************************* Line 12 (18:33:27):: ********************************************************* Line 13 (18:33:27):: * TEST 2 REPORT: SwitchCase "Z" Function Line 14 (18:33:27):: * Most recent 5 runs: Line 15 (18:33:27):: * 1: 2258ms Line 16 (18:33:27):: * 2: 2267ms Line 17 (18:33:27):: * 3: 2258ms Line 18 (18:33:27):: * 4: 2259ms Line 19 (18:33:27):: * 5: 2264ms Line 20 (18:33:27):: *---------------------------------------------------------- Line 21 (18:33:27):: * Average run time: 2259ms - over 6 tests Line 22 (18:33:27):: ********************************************************* Line 23 (18:33:27):: ********************************************************* Line 24 (18:33:27):: * TEST 3 REPORT: SwitchCase "Random" Function Line 25 (18:33:27):: * Most recent 5 runs: Line 26 (18:33:27):: * 1: 2173ms Line 27 (18:33:27):: * 2: 2177ms Line 28 (18:33:27):: * 3: 2174ms Line 29 (18:33:27):: * 4: 2173ms Line 30 (18:33:27):: * 5: 2181ms Line 31 (18:33:27):: *---------------------------------------------------------- Line 32 (18:33:27):: * Average run time: 2173ms - over 6 tests Line 33 (18:33:27):: ********************************************************* Line 34 (18:33:27):: ********************************************************* Line 35 (18:33:27):: * TEST 4 REPORT: SwitchCase "Random" Call Line 36 (18:33:27):: * Most recent 5 runs: Line 37 (18:33:27):: * 1: 2202ms Line 38 (18:33:27):: * 2: 2208ms Line 39 (18:33:27):: * 3: 2204ms Line 40 (18:33:27):: * 4: 2208ms Line 41 (18:33:27):: * 5: 2210ms Line 42 (18:33:27):: *---------------------------------------------------------- Line 43 (18:33:27):: * Average run time: 2205ms - over 6 tests Line 44 (18:33:27):: ********************************************************* Line 45 (18:33:27):: ********************************************************* Line 46 (18:33:27):: * TEST 5 REPORT: SelectActive "A" Function Line 47 (18:33:27):: * Most recent 5 runs: Line 48 (18:33:27):: * 1: 2381ms Line 49 (18:33:27):: * 2: 2381ms Line 50 (18:33:27):: * 3: 2382ms Line 51 (18:33:27):: * 4: 2379ms Line 52 (18:33:27):: * 5: 2384ms Line 53 (18:33:27):: *---------------------------------------------------------- Line 54 (18:33:27):: * Average run time: 2381ms - over 6 tests Line 55 (18:33:27):: ********************************************************* Line 56 (18:33:27):: ********************************************************* Line 57 (18:33:27):: * TEST 6 REPORT: SelectActive "Z" Function Line 58 (18:33:27):: * Most recent 5 runs: Line 59 (18:33:27):: * 1: 2612ms Line 60 (18:33:27):: * 2: 2614ms Line 61 (18:33:27):: * 3: 2613ms Line 62 (18:33:27):: * 4: 2614ms Line 63 (18:33:27):: * 5: 2618ms Line 64 (18:33:27):: *---------------------------------------------------------- Line 65 (18:33:27):: * Average run time: 2613ms - over 6 tests Line 66 (18:33:27):: ********************************************************* Line 67 (18:33:27):: ********************************************************* Line 68 (18:33:27):: * TEST 7 REPORT: SelectActive "Random" Function Line 69 (18:33:27):: * Most recent 5 runs: Line 70 (18:33:27):: * 1: 2596ms Line 71 (18:33:27):: * 2: 2595ms Line 72 (18:33:27):: * 3: 2596ms Line 73 (18:33:27):: * 4: 2594ms Line 74 (18:33:27):: * 5: 2599ms Line 75 (18:33:27):: *---------------------------------------------------------- Line 76 (18:33:27):: * Average run time: 2595ms - over 6 tests Line 77 (18:33:27):: ********************************************************* Line 78 (18:33:27):: ********************************************************* Line 79 (18:33:27):: * TEST 8 REPORT: SelectActive "Random" Call Line 80 (18:33:27):: * Most recent 5 runs: Line 81 (18:33:27):: * 1: 2570ms Line 82 (18:33:27):: * 2: 2571ms Line 83 (18:33:27):: * 3: 2571ms Line 84 (18:33:27):: * 4: 2571ms Line 85 (18:33:27):: * 5: 2572ms Line 86 (18:33:27):: *---------------------------------------------------------- Line 87 (18:33:27):: * Average run time: 2570ms - over 6 tests Line 88 (18:33:27):: ********************************************************* Line 89 (18:33:27):: ********************************************************* Line 90 (18:33:27):: * TEST 9 REPORT: IfElseIf "A" Function Line 91 (18:33:27):: * Most recent 5 runs: Line 92 (18:33:27):: * 1: 2373ms Line 93 (18:33:27):: * 2: 2372ms Line 94 (18:33:27):: * 3: 2373ms Line 95 (18:33:27):: * 4: 2373ms Line 96 (18:33:27):: * 5: 2378ms Line 97 (18:33:27):: *---------------------------------------------------------- Line 98 (18:33:27):: * Average run time: 2373ms - over 6 tests Line 99 (18:33:27):: ********************************************************* Line 100 (18:33:27):: ********************************************************* Line 101 (18:33:27):: * TEST 10 REPORT: IfElseIf "Z" Function Line 102 (18:33:27):: * Most recent 5 runs: Line 103 (18:33:27):: * 1: 2606ms Line 104 (18:33:27):: * 2: 2606ms Line 105 (18:33:27):: * 3: 2607ms Line 106 (18:33:27):: * 4: 2606ms Line 107 (18:33:27):: * 5: 2611ms Line 108 (18:33:27):: *---------------------------------------------------------- Line 109 (18:33:27):: * Average run time: 2606ms - over 6 tests Line 110 (18:33:27):: ********************************************************* Line 111 (18:33:27):: ********************************************************* Line 112 (18:33:27):: * TEST 11 REPORT: IfElseIf "Random" Function Line 113 (18:33:27):: * Most recent 5 runs: Line 114 (18:33:27):: * 1: 2581ms Line 115 (18:33:27):: * 2: 2580ms Line 116 (18:33:27):: * 3: 2581ms Line 117 (18:33:27):: * 4: 2580ms Line 118 (18:33:27):: * 5: 2587ms Line 119 (18:33:27):: *---------------------------------------------------------- Line 120 (18:33:27):: * Average run time: 2581ms - over 6 tests Line 121 (18:33:27):: ********************************************************* Line 122 (18:33:27):: ********************************************************* Line 123 (18:33:27):: * TEST 12 REPORT: IfElseIf "Random" Call Line 124 (18:33:27):: * Most recent 5 runs: Line 125 (18:33:27):: * 1: 2588ms Line 126 (18:33:27):: * 2: 2587ms Line 127 (18:33:27):: * 3: 2586ms Line 128 (18:33:27):: * 4: 2586ms Line 129 (18:33:27):: * 5: 2586ms Line 130 (18:33:27):: *---------------------------------------------------------- Line 131 (18:33:27):: * Average run time: 2586ms - over 6 tests Line 132 (18:33:27):: ********************************************************* Line 133 (18:33:27):: ********************************************************* Line 134 (18:33:27):: * TEST 13 REPORT: If "A" Function Line 135 (18:33:27):: * Most recent 5 runs: Line 136 (18:33:27):: * 1: 2752ms Line 137 (18:33:27):: * 2: 2754ms Line 138 (18:33:27):: * 3: 2755ms Line 139 (18:33:27):: * 4: 2754ms Line 140 (18:33:27):: * 5: 2755ms Line 141 (18:33:27):: *---------------------------------------------------------- Line 142 (18:33:27):: * Average run time: 2753ms - over 6 tests Line 143 (18:33:27):: ********************************************************* Line 144 (18:33:27):: ********************************************************* Line 145 (18:33:27):: * TEST 14 REPORT: If "Z" Function Line 146 (18:33:27):: * Most recent 5 runs: Line 147 (18:33:27):: * 1: 2752ms Line 148 (18:33:27):: * 2: 2753ms Line 149 (18:33:27):: * 3: 2751ms Line 150 (18:33:27):: * 4: 2753ms Line 151 (18:33:27):: * 5: 2751ms Line 152 (18:33:27):: *---------------------------------------------------------- Line 153 (18:33:27):: * Average run time: 2751ms - over 6 tests Line 154 (18:33:27):: ********************************************************* Line 155 (18:33:27):: ********************************************************* Line 156 (18:33:27):: * TEST 15 REPORT: If "Random" Function Line 157 (18:33:27):: * Most recent 5 runs: Line 158 (18:33:27):: * 1: 2861ms Line 159 (18:33:27):: * 2: 2861ms Line 160 (18:33:27):: * 3: 2862ms Line 161 (18:33:27):: * 4: 2861ms Line 162 (18:33:27):: * 5: 2862ms Line 163 (18:33:27):: *---------------------------------------------------------- Line 164 (18:33:27):: * Average run time: 2861ms - over 6 tests Line 165 (18:33:27):: *********************************************************
It seems that Switch Case wins on a single character. Select Active and If Else If are almost identical. The big surprise to me is that multiple If statements are slower when the data coming to them is random. I thought for sure that each if() statement would need to be evaluated and since one of them is always evaluating to true, it should take the same amount of time for each run through regardless of the result.
The timing on the first three methods follows the patterns I was thinking they should.
If you would like a copy of the code to play with, let me know and I'll post it here.
I am going to check the differences between a function and a call real quick
Jeff
Comments
-
My guess on the IF statements would be that there is an internal optimization done by the compiler so that it is not actually evaluated again if the parameters haven't changed. It seems to me that would be an optimization well worth making in any case.
-
DHawthorne wrote: »My guess on the IF statements would be that there is an internal optimization done by the compiler so that it is not actually evaluated again if the parameters haven't changed. It seems to me that would be an optimization well worth making in any case.
I would agree if I were using a single variable, but I am using an array and setting each element of the array to the letter A. Altho, now that I think about it, maybe it is a matter of the processor caching the code block to be run? I'm thinking if I create an array where I use blocks of the same letter, the time should be close to the same as all one letter.
Back in a few.
Jeff -
As I was getting ready to test the last thought, I found an oversight in my code. It seems that I forgot to change the code that initializes the data arrays to initialize the entire array. I had it set to 1000 instead of referencing the constant that dictates the size of the array. This meant that after the first 1000 values, everything else was not necessarily a value of 'A' to 'Z'.
Because of this, the tests were not run as I expected them to run because after the first 1000 iterations, all of the methods were probably going to the default condition which is the last condition. The new test results seem more inline with what I was expecting.
I think this was what caused the multiple if statements method to vary between the random chars and fixed chars. The last if statement looks like this:if(sData[x] < 'A' and sData[x] > 'Z') nInfo[x] = 0;
I think the data in the arrays caused the fixed char arrays to fail the first condition in the if statement and as a result, the second condition was never tested because of the AND. This raises some more questions about if the processor is able to stop evaluating a condition as soon as it cannot be true.
Regardless, here are the new results.Line 1 (09:34:41):: ********************************************************* Line 2 (09:34:41):: * TEST 1 REPORT: SwitchCase "A" Function Line 3 (09:34:41):: * Most recent 5 runs: Line 4 (09:34:41):: * 1: 566ms Line 5 (09:34:41):: * 2: 567ms Line 6 (09:34:41):: * 3: 567ms Line 7 (09:34:41):: * 4: 566ms Line 8 (09:34:41):: * 5: 569ms Line 9 (09:34:41):: *---------------------------------------------------------- Line 10 (09:34:41):: * Average run time: 566ms - over 5 tests Line 11 (09:34:41):: ********************************************************* Line 12 (09:34:41):: ********************************************************* Line 13 (09:34:41):: * TEST 2 REPORT: SwitchCase "Z" Function Line 14 (09:34:41):: * Most recent 5 runs: Line 15 (09:34:41):: * 1: 2266ms Line 16 (09:34:41):: * 2: 2263ms Line 17 (09:34:41):: * 3: 2266ms Line 18 (09:34:41):: * 4: 2266ms Line 19 (09:34:41):: * 5: 2265ms Line 20 (09:34:41):: *---------------------------------------------------------- Line 21 (09:34:41):: * Average run time: 2264ms - over 5 tests Line 22 (09:34:41):: ********************************************************* Line 23 (09:34:41):: ********************************************************* Line 24 (09:34:41):: * TEST 3 REPORT: SwitchCase "Random" Function Line 25 (09:34:41):: * Most recent 5 runs: Line 26 (09:34:41):: * 1: 1429ms Line 27 (09:34:41):: * 2: 1429ms Line 28 (09:34:41):: * 3: 1430ms Line 29 (09:34:41):: * 4: 1427ms Line 30 (09:34:41):: * 5: 1428ms Line 31 (09:34:41):: *---------------------------------------------------------- Line 32 (09:34:41):: * Average run time: 1428ms - over 5 tests Line 33 (09:34:41):: ********************************************************* Line 34 (09:34:41):: ********************************************************* Line 35 (09:34:41):: * TEST 4 REPORT: SwitchCase "Random" Call Line 36 (09:34:41):: * Most recent 5 runs: Line 37 (09:34:41):: * 1: 1427ms Line 38 (09:34:41):: * 2: 1426ms Line 39 (09:34:41):: * 3: 1427ms Line 40 (09:34:41):: * 4: 1426ms Line 41 (09:34:41):: * 5: 1428ms Line 42 (09:34:41):: *---------------------------------------------------------- Line 43 (09:34:41):: * Average run time: 1426ms - over 5 tests Line 44 (09:34:41):: ********************************************************* Line 45 (09:34:41):: ********************************************************* Line 46 (09:34:41):: * TEST 5 REPORT: SelectActive "A" Function Line 47 (09:34:41):: * Most recent 5 runs: Line 48 (09:34:41):: * 1: 259ms Line 49 (09:34:41):: * 2: 260ms Line 50 (09:34:41):: * 3: 258ms Line 51 (09:34:41):: * 4: 261ms Line 52 (09:34:41):: * 5: 259ms Line 53 (09:34:41):: *---------------------------------------------------------- Line 54 (09:34:41):: * Average run time: 258ms - over 5 tests Line 55 (09:34:41):: ********************************************************* Line 56 (09:34:41):: ********************************************************* Line 57 (09:34:41):: * TEST 6 REPORT: SelectActive "Z" Function Line 58 (09:34:41):: * Most recent 5 runs: Line 59 (09:34:41):: * 1: 2598ms Line 60 (09:34:41):: * 2: 2602ms Line 61 (09:34:41):: * 3: 2605ms Line 62 (09:34:41):: * 4: 2599ms Line 63 (09:34:41):: * 5: 2601ms Line 64 (09:34:41):: *---------------------------------------------------------- Line 65 (09:34:41):: * Average run time: 2600ms - over 5 tests Line 66 (09:34:41):: ********************************************************* Line 67 (09:34:41):: ********************************************************* Line 68 (09:34:41):: * TEST 7 REPORT: SelectActive "Random" Function Line 69 (09:34:41):: * Most recent 5 runs: Line 70 (09:34:41):: * 1: 1442ms Line 71 (09:34:41):: * 2: 1446ms Line 72 (09:34:41):: * 3: 1445ms Line 73 (09:34:41):: * 4: 1443ms Line 74 (09:34:41):: * 5: 1442ms Line 75 (09:34:41):: *---------------------------------------------------------- Line 76 (09:34:41):: * Average run time: 1442ms - over 5 tests Line 77 (09:34:41):: ********************************************************* Line 78 (09:34:41):: ********************************************************* Line 79 (09:34:41):: * TEST 8 REPORT: SelectActive "Random" Call Line 80 (09:34:41):: * Most recent 5 runs: Line 81 (09:34:41):: * 1: 1433ms Line 82 (09:34:41):: * 2: 1433ms Line 83 (09:34:41):: * 3: 1433ms Line 84 (09:34:41):: * 4: 1433ms Line 85 (09:34:41):: * 5: 1434ms Line 86 (09:34:41):: *---------------------------------------------------------- Line 87 (09:34:41):: * Average run time: 1433ms - over 5 tests Line 88 (09:34:41):: ********************************************************* Line 89 (09:34:41):: ********************************************************* Line 90 (09:34:41):: * TEST 9 REPORT: IfElseIf "A" Function Line 91 (09:34:41):: * Most recent 5 runs: Line 92 (09:34:41):: * 1: 254ms Line 93 (09:34:41):: * 2: 255ms Line 94 (09:34:41):: * 3: 257ms Line 95 (09:34:41):: * 4: 254ms Line 96 (09:34:41):: * 5: 254ms Line 97 (09:34:41):: *---------------------------------------------------------- Line 98 (09:34:41):: * Average run time: 254ms - over 5 tests Line 99 (09:34:41):: ********************************************************* Line 100 (09:34:41):: ********************************************************* Line 101 (09:34:41):: * TEST 10 REPORT: IfElseIf "Z" Function Line 102 (09:34:41):: * Most recent 5 runs: Line 103 (09:34:41):: * 1: 2607ms Line 104 (09:34:41):: * 2: 2606ms Line 105 (09:34:41):: * 3: 2607ms Line 106 (09:34:41):: * 4: 2606ms Line 107 (09:34:41):: * 5: 2606ms Line 108 (09:34:41):: *---------------------------------------------------------- Line 109 (09:34:41):: * Average run time: 2606ms - over 5 tests Line 110 (09:34:41):: ********************************************************* Line 111 (09:34:41):: ********************************************************* Line 112 (09:34:41):: * TEST 11 REPORT: IfElseIf "Random" Function Line 113 (09:34:41):: * Most recent 5 runs: Line 114 (09:34:41):: * 1: 1439ms Line 115 (09:34:41):: * 2: 1440ms Line 116 (09:34:41):: * 3: 1440ms Line 117 (09:34:41):: * 4: 1439ms Line 118 (09:34:41):: * 5: 1440ms Line 119 (09:34:41):: *---------------------------------------------------------- Line 120 (09:34:41):: * Average run time: 1439ms - over 5 tests Line 121 (09:34:41):: ********************************************************* Line 122 (09:34:41):: ********************************************************* Line 123 (09:34:41):: * TEST 12 REPORT: IfElseIf "Random" Call Line 124 (09:34:41):: * Most recent 5 runs: Line 125 (09:34:41):: * 1: 1446ms Line 126 (09:34:41):: * 2: 1448ms Line 127 (09:34:41):: * 3: 1448ms Line 128 (09:34:41):: * 4: 1446ms Line 129 (09:34:41):: * 5: 1446ms Line 130 (09:34:41):: *---------------------------------------------------------- Line 131 (09:34:41):: * Average run time: 1446ms - over 5 tests Line 132 (09:34:41):: ********************************************************* Line 133 (09:34:41):: ********************************************************* Line 134 (09:34:41):: * TEST 13 REPORT: If "A" Function Line 135 (09:34:41):: * Most recent 5 runs: Line 136 (09:34:41):: * 1: 2813ms Line 137 (09:34:41):: * 2: 2814ms Line 138 (09:34:41):: * 3: 2813ms Line 139 (09:34:41):: * 4: 2813ms Line 140 (09:34:41):: * 5: 2811ms Line 141 (09:34:41):: *---------------------------------------------------------- Line 142 (09:34:41):: * Average run time: 2812ms - over 5 tests Line 143 (09:34:41):: ********************************************************* Line 144 (09:34:41):: ********************************************************* Line 145 (09:34:41):: * TEST 14 REPORT: If "Z" Function Line 146 (09:34:41):: * Most recent 5 runs: Line 147 (09:34:41):: * 1: 2810ms Line 148 (09:34:41):: * 2: 2810ms Line 149 (09:34:41):: * 3: 2810ms Line 150 (09:34:41):: * 4: 2812ms Line 151 (09:34:41):: * 5: 2811ms Line 152 (09:34:41):: *---------------------------------------------------------- Line 153 (09:34:41):: * Average run time: 2810ms - over 5 tests Line 154 (09:34:41):: ********************************************************* Line 155 (09:34:41):: ********************************************************* Line 156 (09:34:41):: * TEST 15 REPORT: If "Random" Function Line 157 (09:34:41):: * Most recent 5 runs: Line 158 (09:34:41):: * 1: 2811ms Line 159 (09:34:41):: * 2: 2813ms Line 160 (09:34:41):: * 3: 2813ms Line 161 (09:34:41):: * 4: 2812ms Line 162 (09:34:41):: * 5: 2813ms Line 163 (09:34:41):: *---------------------------------------------------------- Line 164 (09:34:41):: * Average run time: 2812ms - over 5 tests Line 165 (09:34:41):: *********************************************************
As you can see, in a real world situation where data is random, there is little difference between switch..case, select..active, and if..else..if. Interestingly enough, it seems that select..active and ifElseIf are faster than switch..case when the majority of values are the first condition, but slower when the majority of the values are the last condition.
This reinforces my tendency to put conditions I think will occur most often first. Think about a device like a security panel. It would make sense to check for a status/blink message as the first condition, then maybe put zone status messages next, then put fire/burglary messages at the end of the line.
Jeff -
I completely agree. Select Active, Switch Case and if/else should always be constructed in a hierarchical order since they will all exit upon first match. It only makes sense to put conditions that will match more frequently at the top and conditons that will match less frequently at the bottom.Spire_Jeff wrote:This reinforces my tendency to put conditions I think will occur most often first. -
I completely agree. Select Active, Switch Case and if/else should always be constructed in a hierarchical order since they will all exit upon first match. It only makes sense to put conditions that will match more frequently at the top and conditons that will match less frequently at the bottom.
Our instructor at PIII last August said that Switch/Case is actually a pretty spendy process. He mad eht statement that they found no significant differences between the methods of conditional searching. Of course, it's a little hard to compare apples to apples.
I myself tend to use them based upon what strength they offer in the search I'm trying to accomplish. -
What exactly did he mean by that? I haven't taken that close of a look at Spire_Jeff's test results but I thought it showed the three methods were reasonably similar in processing time.ericmedley wrote:Our instructor at PIII last August said that Switch/Case is actually a pretty spendy process.
I don't understand how that can be. If you have a switch with 100 cases in a parsing function and 99% of the time your parsing a KLS string for Lutron for example, putting the test for the KLS string in the last case position would obviously be waisting time since 99% of the time it will have to test 99 cases prior to getting to the case which tests for the KLS. It only makes sense to put that in the first case position and something like LOGIN: or login successful should be at the bottom of the switch since those are only tested once upon connection.ericmedley wrote:He mad eht statement that they found no significant differences between the methods of conditional searching. -
I think the test results may change if I use strings instead of chars as the switch case arguments. If I have some time, I will try it out and see which is the better way to handle strings.
Jeff -
What exactly did he mean by that? I haven't taken that close of a look at Spire_Jeff's test results but I thought it showed the three methods were reasonably similar in processing time.
I'm not sure. It was just a curiosity at the time. I remember back in my old days of computer engineering and processor/compiler design that conditionals are notoriously time consuming. But we have to do them.
As I said, I don't get too involved with how well the engine is running myself. I try to pick the method that makes the most programming sense to me. I'm not against using IF/ELSE if it works out to be the best method. I like to use it when you may have more than one condition be true in a given pass. I cannot remember in Netlinx but in other languages Switch/Case and/or Select Active jump out once a condition is met. There are times when you can get more than one set of data out of a pass by allowing for more than one case to be true. -
ericmedley wrote: »I'm not against using IF/ELSE if it works out to be the best method. I like to use it when you may have more than one condition be true in a given pass. I cannot remember in Netlinx but in other languages Switch/Case and/or Select Active jump out once a condition is met. There are times when you can get more than one set of data out of a pass by allowing for more than one case to be true.
There are two ways to use the if to accomplish the same thing as a select/active. There isif(a) do_1 if(b) do_2 if(c) do_3 if(d) do_4
This method checks every statement all of the time.
Then there is this:if(a) do_1 else if(b) do_2 else if(c) do_3 else if(d) do_4
The second way work just like select/active. In java, there is no select/active (that I am aware of), you have to use this method.
As for other languages, in some of them, the switch case statement only jumps out if you include a break command at the end of the case. This is something that I wish NetLinx would allow as sometimes it would be helpful. I do understand that in most situations it is not the desired behavior, and I can understand why they automatically break the case.
Jeff -
Ok, I just tested the various methods of parsing string data. According to the results, switch case is the most efficient method. This was tested on a random set of strings. Interestingly enough, putting full strings of varying length as the cases in a switch case was faster than using only the first three characters. I also tried switching based on the first character, then the second char if needed. Switching based on the entire string was still faster.
Line 1 (14:50:41):: ********************************************************* Line 2 (14:50:41):: * TEST 1 REPORT: Parse String SelectActive == Line 3 (14:50:41):: * Most recent 5 runs: Line 4 (14:50:41):: * 1: 1254ms Line 5 (14:50:41):: * 2: 1254ms Line 6 (14:50:41):: * 3: 1254ms Line 7 (14:50:41):: * 4: 1254ms Line 8 (14:50:41):: * 5: 1252ms Line 9 (14:50:41):: *---------------------------------------------------------- Line 10 (14:50:41):: * Average run time: 1253ms - over 5 tests Line 11 (14:50:41):: ********************************************************* Line 12 (14:50:41):: ********************************************************* Line 13 (14:50:41):: * TEST 2 REPORT: Parse String SelectActive find() Line 14 (14:50:41):: * Most recent 5 runs: Line 15 (14:50:41):: * 1: 6549ms Line 16 (14:50:41):: * 2: 6450ms Line 17 (14:50:41):: * 3: 6485ms Line 18 (14:50:41):: * 4: 6447ms Line 19 (14:50:41):: * 5: 6480ms Line 20 (14:50:41):: *---------------------------------------------------------- Line 21 (14:50:41):: * Average run time: 6481ms - over 5 tests Line 22 (14:50:41):: ********************************************************* Line 23 (14:50:41):: ********************************************************* Line 24 (14:50:41):: * TEST 3 REPORT: Parse String SwitchCase Random Line 25 (14:50:41):: * Most recent 5 runs: Line 26 (14:50:41):: * 1: 1068ms Line 27 (14:50:41):: * 2: 1066ms Line 28 (14:50:41):: * 3: 1066ms Line 29 (14:50:41):: * 4: 1067ms Line 30 (14:50:41):: * 5: 1068ms Line 31 (14:50:41):: *---------------------------------------------------------- Line 32 (14:50:41):: * Average run time: 1066ms - over 5 tests Line 33 (14:50:41):: ********************************************************* Line 34 (14:50:41):: ********************************************************* Line 35 (14:50:41):: * TEST 4 REPORT: Parse String SwitchCase short->long Line 36 (14:50:41):: * Most recent 5 runs: Line 37 (14:50:41):: * 1: 1074ms Line 38 (14:50:41):: * 2: 1073ms Line 39 (14:50:41):: * 3: 1073ms Line 40 (14:50:41):: * 4: 1072ms Line 41 (14:50:41):: * 5: 1072ms Line 42 (14:50:41):: *---------------------------------------------------------- Line 43 (14:50:41):: * Average run time: 1072ms - over 5 tests Line 44 (14:50:41):: ********************************************************* Line 45 (14:50:41):: ********************************************************* Line 46 (14:50:41):: * TEST 5 REPORT: Parse String SwitchCase long->short Line 47 (14:50:41):: * Most recent 5 runs: Line 48 (14:50:41):: * 1: 1052ms Line 49 (14:50:41):: * 2: 1053ms Line 50 (14:50:41):: * 3: 1052ms Line 51 (14:50:41):: * 4: 1052ms Line 52 (14:50:41):: * 5: 1052ms Line 53 (14:50:41):: *---------------------------------------------------------- Line 54 (14:50:41):: * Average run time: 1052ms - over 5 tests Line 55 (14:50:41):: ********************************************************* Line 56 (14:50:41):: ********************************************************* Line 57 (14:50:41):: * TEST 6 REPORT: Parse String SwitchCase 3Char Line 58 (14:50:41):: * Most recent 5 runs: Line 59 (14:50:41):: * 1: 1557ms Line 60 (14:50:41):: * 2: 1557ms Line 61 (14:50:41):: * 3: 1557ms Line 62 (14:50:41):: * 4: 1556ms Line 63 (14:50:41):: * 5: 1557ms Line 64 (14:50:41):: *---------------------------------------------------------- Line 65 (14:50:41):: * Average run time: 1556ms - over 5 tests Line 66 (14:50:41):: ********************************************************* Line 67 (14:50:41):: ********************************************************* Line 68 (14:50:41):: * TEST 7 REPORT: Parse String SelectActive == 3Char Line 69 (14:50:41):: * Most recent 5 runs: Line 70 (14:50:41):: * 1: 5177ms Line 71 (14:50:41):: * 2: 5183ms Line 72 (14:50:41):: * 3: 5196ms Line 73 (14:50:41):: * 4: 5174ms Line 74 (14:50:41):: * 5: 5176ms Line 75 (14:50:41):: *---------------------------------------------------------- Line 76 (14:50:41):: * Average run time: 5180ms - over 5 tests Line 77 (14:50:41):: ********************************************************* Line 78 (14:50:41):: ********************************************************* Line 79 (14:50:41):: * TEST 8 REPORT: Parse String SelectActive == stack 3char Line 80 (14:50:41):: * Most recent 5 runs: Line 81 (14:50:41):: * 1: 1571ms Line 82 (14:50:41):: * 2: 1571ms Line 83 (14:50:41):: * 3: 1572ms Line 84 (14:50:41):: * 4: 1569ms Line 85 (14:50:41):: * 5: 1576ms Line 86 (14:50:41):: *---------------------------------------------------------- Line 87 (14:50:41):: * Average run time: 1571ms - over 5 tests Line 88 (14:50:41):: ********************************************************* Line 89 (16:17:10):: ********************************************************* Line 90 (16:17:10):: * TEST 9 REPORT: Parse String SwitchCase 3Char Line 91 (16:17:10):: * Most recent 5 runs: Line 92 (16:17:10):: * 1: 1117ms Line 93 (16:17:10):: * 2: 1110ms Line 94 (16:17:10):: * 3: 1110ms Line 95 (16:17:10):: * 4: 1113ms Line 96 (16:17:10):: * 5: 1109ms Line 97 (16:17:10):: *---------------------------------------------------------- Line 98 (16:17:10):: * Average run time: 1111ms - over 5 tests Line 99 (16:17:10):: *********************************************************
For those interested, here are the functions used in the test:define_function parseStringSelectEqual(){//Test 1 stack_var integer x; for(x=MAX_CONDITIONAL;x;x--){ select{ active(sRandomStrings[x] == 'Dog'): nInfo[x] = 1; active(sRandomStrings[x] == 'Cat'): nInfo[x] = 2; active(sRandomStrings[x] == 'Wolf'): nInfo[x] = 3; active(sRandomStrings[x] == 'Long Animal Names'): nInfo[x] = 4; active(sRandomStrings[x] == 'Elephant'): nInfo[x] = 5; active(sRandomStrings[x] == 'Emu'): nInfo[x] = 6; active(sRandomStrings[x] == 'Whale'): nInfo[x] = 7; active(sRandomStrings[x] == 'Aardvark'): nInfo[x] = 8; active(sRandomStrings[x] == 'Ostrich'): nInfo[x] = 9; active(sRandomStrings[x] == 'Coyote'): nInfo[x] = 10; active(sRandomStrings[x] == 'Monkey'): nInfo[x] = 11; active(sRandomStrings[x] == 'Rhinoceros'): nInfo[x] = 12; active(sRandomStrings[x] == 'Kangaroo'): nInfo[x] = 13; active(sRandomStrings[x] == 'Penguin'): nInfo[x] = 14; active(sRandomStrings[x] == 'Seal'): nInfo[x] = 15; active(sRandomStrings[x] == 'Lion'): nInfo[x] = 16; active(sRandomStrings[x] == 'Tiger'): nInfo[x] = 17; active(sRandomStrings[x] == 'Another Long Name'): nInfo[x] = 18; active(sRandomStrings[x] == 'Lizard'): nInfo[x] = 19; active(sRandomStrings[x] == 'Boa Constrictor'): nInfo[x] = 20; active(1): send_string 0,"'Problem in call'" } } } define_function parseStringSelectFind(){//test 2 stack_var integer x; for(x=MAX_CONDITIONAL;x;x--){ select{ active(find_string(sRandomStrings[x],'Dog',1)): nInfo[x] = 1; active(find_string(sRandomStrings[x],'Cat',1)): nInfo[x] = 2; active(find_string(sRandomStrings[x],'Wolf',1)): nInfo[x] = 3; active(find_string(sRandomStrings[x],'Long Animal Names',1)): nInfo[x] = 4; active(find_string(sRandomStrings[x],'Elephant',1)): nInfo[x] = 5; active(find_string(sRandomStrings[x],'Emu',1)): nInfo[x] = 6; active(find_string(sRandomStrings[x],'Whale',1)): nInfo[x] = 7; active(find_string(sRandomStrings[x],'Aardvark',1)): nInfo[x] = 8; active(find_string(sRandomStrings[x],'Ostrich',1)): nInfo[x] = 9; active(find_string(sRandomStrings[x],'Coyote',1)): nInfo[x] = 10; active(find_string(sRandomStrings[x],'Monkey',1)): nInfo[x] = 11; active(find_string(sRandomStrings[x],'Rhinoceros',1)): nInfo[x] = 12; active(find_string(sRandomStrings[x],'Kangaroo',1)): nInfo[x] = 13; active(find_string(sRandomStrings[x],'Penguin',1)): nInfo[x] = 14; active(find_string(sRandomStrings[x],'Seal',1)): nInfo[x] = 15; active(find_string(sRandomStrings[x],'Lion',1)): nInfo[x] = 16; active(find_string(sRandomStrings[x],'Tiger',1)): nInfo[x] = 17; active(find_string(sRandomStrings[x],'Another Long Name',1)): nInfo[x] = 18; active(find_string(sRandomStrings[x],'Lizard',1)): nInfo[x] = 19; active(find_string(sRandomStrings[x],'Boa Constrictor',1)): nInfo[x] = 20; active(1): send_string 0,"'Problem in call'" } } } define_function parseStringSwitchCase1(){//test 3 stack_var integer x; for(x=MAX_CONDITIONAL;x;x--){ switch(sRandomStrings[x]){ case 'Dog': nInfo[x] = 1; case 'Cat': nInfo[x] = 2; case 'Wolf': nInfo[x] = 3; case 'Long Animal Names': nInfo[x] = 4; case 'Elephant': nInfo[x] = 5; case 'Emu': nInfo[x] = 6; case 'Whale': nInfo[x] = 7; case 'Aardvark': nInfo[x] = 8; case 'Ostrich': nInfo[x] = 9; case 'Coyote': nInfo[x] = 10; case 'Monkey': nInfo[x] = 11; case 'Rhinoceros': nInfo[x] = 12; case 'Kangaroo': nInfo[x] = 13; case 'Penguin': nInfo[x] = 14; case 'Seal': nInfo[x] = 15; case 'Lion': nInfo[x] = 16; case 'Tiger': nInfo[x] = 17; case 'Another Long Name': nInfo[x] = 18; case 'Lizard': nInfo[x] = 19; case 'Boa Constrictor': nInfo[x] = 20; default: send_string 0,"'Problem in call'" } } } define_function parseStringSwitchCase2(){/test 4 stack_var integer x; for(x=MAX_CONDITIONAL;x;x--){ switch(sRandomStrings[x]){ case 'Dog': nInfo[x] = 1; case 'Cat': nInfo[x] = 2; case 'Emu': nInfo[x] = 6; case 'Wolf': nInfo[x] = 3; case 'Seal': nInfo[x] = 15; case 'Lion': nInfo[x] = 16; case 'Whale': nInfo[x] = 7; case 'Tiger': nInfo[x] = 17; case 'Coyote': nInfo[x] = 10; case 'Monkey': nInfo[x] = 11; case 'Lizard': nInfo[x] = 19; case 'Ostrich': nInfo[x] = 9; case 'Penguin': nInfo[x] = 14; case 'Aardvark': nInfo[x] = 8; case 'Elephant': nInfo[x] = 5; case 'Kangaroo': nInfo[x] = 13; case 'Rhinoceros': nInfo[x] = 12; case 'Boa Constrictor': nInfo[x] = 20; case 'Another Long Name': nInfo[x] = 18; case 'Long Animal Names': nInfo[x] = 4; default: send_string 0,"'Problem in call'" } } } define_function parseStringSwitchCase3(){//test 5 stack_var integer x; for(x=MAX_CONDITIONAL;x;x--){ switch(sRandomStrings[x]){ case 'Another Long Name': nInfo[x] = 18; case 'Long Animal Names': nInfo[x] = 4; case 'Boa Constrictor': nInfo[x] = 20; case 'Rhinoceros': nInfo[x] = 12; case 'Aardvark': nInfo[x] = 8; case 'Elephant': nInfo[x] = 5; case 'Kangaroo': nInfo[x] = 13; case 'Ostrich': nInfo[x] = 9; case 'Penguin': nInfo[x] = 14; case 'Coyote': nInfo[x] = 10; case 'Monkey': nInfo[x] = 11; case 'Lizard': nInfo[x] = 19; case 'Whale': nInfo[x] = 7; case 'Tiger': nInfo[x] = 17; case 'Wolf': nInfo[x] = 3; case 'Seal': nInfo[x] = 15; case 'Lion': nInfo[x] = 16; case 'Dog': nInfo[x] = 1; case 'Cat': nInfo[x] = 2; case 'Emu': nInfo[x] = 6; default: send_string 0,"'Problem in call'" } } } define_function parseStringSwitchCase4(){//test 6 stack_var integer x; for(x=MAX_CONDITIONAL;x;x--){ switch(left_string(sRandomStrings[x],3)){ case 'Dog': nInfo[x] = 1; case 'Cat': nInfo[x] = 2; case 'Wol': nInfo[x] = 3; case 'Lon': nInfo[x] = 4; case 'Ele': nInfo[x] = 5; case 'Emu': nInfo[x] = 6; case 'Wha': nInfo[x] = 7; case 'Aar': nInfo[x] = 8; case 'Ost': nInfo[x] = 9; case 'Coy': nInfo[x] = 10; case 'Mon': nInfo[x] = 11; case 'Rhi': nInfo[x] = 12; case 'Kan': nInfo[x] = 13; case 'Pen': nInfo[x] = 14; case 'Sea': nInfo[x] = 15; case 'Lio': nInfo[x] = 16; case 'Tig': nInfo[x] = 17; case 'Ano': nInfo[x] = 18; case 'Liz': nInfo[x] = 19; case 'Boa': nInfo[x] = 20; default: send_string 0,"'Problem in call'" } } } define_function parseStringSelectEqualLeft1(){//test 7 stack_var integer x; for(x=MAX_CONDITIONAL;x;x--){ select{ active(left_string(sRandomStrings[x],3) == 'Dog'): nInfo[x] = 1; active(left_string(sRandomStrings[x],3) == 'Cat'): nInfo[x] = 2; active(left_string(sRandomStrings[x],3) == 'Wol'): nInfo[x] = 3; active(left_string(sRandomStrings[x],3) == 'Lon'): nInfo[x] = 4; active(left_string(sRandomStrings[x],3) == 'Ele'): nInfo[x] = 5; active(left_string(sRandomStrings[x],3) == 'Emu'): nInfo[x] = 6; active(left_string(sRandomStrings[x],3) == 'Wha'): nInfo[x] = 7; active(left_string(sRandomStrings[x],3) == 'Aar'): nInfo[x] = 8; active(left_string(sRandomStrings[x],3) == 'Ost'): nInfo[x] = 9; active(left_string(sRandomStrings[x],3) == 'Coy'): nInfo[x] = 10; active(left_string(sRandomStrings[x],3) == 'Mon'): nInfo[x] = 11; active(left_string(sRandomStrings[x],3) == 'Rhi'): nInfo[x] = 12; active(left_string(sRandomStrings[x],3) == 'Kan'): nInfo[x] = 13; active(left_string(sRandomStrings[x],3) == 'Pen'): nInfo[x] = 14; active(left_string(sRandomStrings[x],3) == 'Sea'): nInfo[x] = 15; active(left_string(sRandomStrings[x],3) == 'Lio'): nInfo[x] = 16; active(left_string(sRandomStrings[x],3) == 'Tig'): nInfo[x] = 17; active(left_string(sRandomStrings[x],3) == 'Ano'): nInfo[x] = 18; active(left_string(sRandomStrings[x],3) == 'Liz'): nInfo[x] = 19; active(left_string(sRandomStrings[x],3) == 'Boa'): nInfo[x] = 20; active(1): send_string 0,"'Problem in call'" } } } define_function parseStringSelectEqualLeft2(){//test 8 stack_var integer x; stack_var char sData[3]; for(x=MAX_CONDITIONAL;x;x--){ sData = left_string(sRandomStrings[x],3); select{ active(sData == 'Dog'): nInfo[x] = 1; active(sData == 'Cat'): nInfo[x] = 2; active(sData == 'Wol'): nInfo[x] = 3; active(sData == 'Lon'): nInfo[x] = 4; active(sData == 'Ele'): nInfo[x] = 5; active(sData == 'Emu'): nInfo[x] = 6; active(sData == 'Wha'): nInfo[x] = 7; active(sData == 'Aar'): nInfo[x] = 8; active(sData == 'Ost'): nInfo[x] = 9; active(sData == 'Coy'): nInfo[x] = 10; active(sData == 'Mon'): nInfo[x] = 11; active(sData == 'Rhi'): nInfo[x] = 12; active(sData == 'Kan'): nInfo[x] = 13; active(sData == 'Pen'): nInfo[x] = 14; active(sData == 'Sea'): nInfo[x] = 15; active(sData == 'Lio'): nInfo[x] = 16; active(sData == 'Tig'): nInfo[x] = 17; active(sData == 'Ano'): nInfo[x] = 18; active(sData == 'Liz'): nInfo[x] = 19; active(sData == 'Boa'): nInfo[x] = 20; active(1): send_string 0,"'Problem in call'" } } } define_function parseStringSwitchCase5(){//test 9 stack_var integer x; for(x=MAX_CONDITIONAL;x;x--){ switch(sRandomStrings[x][1]){ case 'D': nInfo[x] = 1; case 'C': nInfo[x] = 2; case 'W': nInfo[x] = 3; case 'L': nInfo[x] = 4; case 'E':{ switch(sRandomStrings[x][2]){ case 'l': nInfo[x] = 5; case 'm': nInfo[x] = 6; } } case 'W': nInfo[x] = 7; case 'A':{ switch(sRandomStrings[x][2]){ case 'a': nInfo[x] = 8; case 'n': nInfo[x] = 18; } } case 'O': nInfo[x] = 9; case 'C': nInfo[x] = 10; case 'M': nInfo[x] = 11; case 'R': nInfo[x] = 12; case 'K': nInfo[x] = 13; case 'P': nInfo[x] = 14; case 'S': nInfo[x] = 15; case 'L':{ switch(sRandomStrings[x][3]){ case 'o': nInfo[x] = 16; case 'z': nInfo[x] = 19; } } case 'T': nInfo[x] = 17; case 'B': nInfo[x] = 20; default: send_string 0,"'Problem in call'" } } } DEFINE_START SET_LENGTH_ARRAY (nInfo,70000) for(nLoop2=MAX_CONDITIONAL;nLoop2;nLoop2--){ cAChars[nLoop2] = 'A'; cZChars[nLoop2] = 'Z'; cRandomChars[nLoop2] = RANDOM_NUMBER(26)+65; switch(RANDOM_NUMBER(20)){ case 0: sRandomStrings[nLoop2] = 'Dog' case 1: sRandomStrings[nLoop2] = 'Cat' case 2: sRandomStrings[nLoop2] = 'Wolf' case 3: sRandomStrings[nLoop2] = 'Long Animal Names' case 4: sRandomStrings[nLoop2] = 'Elephant' case 5: sRandomStrings[nLoop2] = 'Emu' case 6: sRandomStrings[nLoop2] = 'Whale' case 7: sRandomStrings[nLoop2] = 'Aardvark' case 8: sRandomStrings[nLoop2] = 'Ostrich' case 9: sRandomStrings[nLoop2] = 'Coyote' case 10: sRandomStrings[nLoop2] = 'Monkey' case 11: sRandomStrings[nLoop2] = 'Rhinoceros' case 12: sRandomStrings[nLoop2] = 'Kangaroo' case 13: sRandomStrings[nLoop2] = 'Penguin' case 14: sRandomStrings[nLoop2] = 'Seal' case 15: sRandomStrings[nLoop2] = 'Lion' case 16: sRandomStrings[nLoop2] = 'Tiger' case 17: sRandomStrings[nLoop2] = 'Another Long Name' case 18: sRandomStrings[nLoop2] = 'Lizard' case 19: sRandomStrings[nLoop2] = 'Boa Constrictor' } }
Jeff
Leave a Comment
Categories
- All Categories
- 2.5K AMX General Discussion
- 922 AMX Technical Discussion
- 514 AMX Hardware
- 502 AMX Control Products
- 3 AMX Video Distribution Products
- 9 AMX Networked AV (SVSI) Products
- AMX Workspace & Collaboration Products
- 3.4K AMX Software
- 151 AMX Resource Management Suite Software
- 386 AMX Design Tools
- 2.4K NetLinx Studio
- 135 Duet/Cafe Duet
- 248 NetLinx Modules & Duet Modules
- 57 AMX RPM Forum
- 228 MODPEDIA - The Public Repository of Modules for Everyone
- 943 AMX Specialty Forums
- 2.6K AMXForums Archive
- 2.6K AMXForums Archive Threads
- 1.5K AMX Hardware
- 432 AMX Applications and Solutions
- 249 Residential Forum
- 182 Tips and Tricks
- 146 AMX Website/Forums
