This analysis was done in 2006. I decide to blog it as from time to time I share the similar info with others who asked me the similar questions.
A colleague ask for my help to analyse 2 sets of code that are doing similar function, but both has signicant different in terms of processing time. Below are the 2 source codes:
Figure1: 1st Code
Figure2: 2nd Code
Basically these functions are doing some bit operation on 2Mbits of data.From his profile results, the 1st code takes about 25-30 seconds. While 2nd code takes about 13-15 seconds to process.
Below are my analysis:
For 1st code, VEE iterates the highlighted formula container for 11million times. Lets calculate the number of operations in this formula container:
1. “B+1”
2. asInt16(A[B+1])
3. bitshift(asInt16(A[B+1]),8)
4. bitshift(asInt16(A[B+1]),8) + A[B]
5. (bitshift(asInt16(A[B+1]),8) + A[B]) * LSB_Volt
So, we are executing 5 * 1M = 5 million of operation in the For Range itself.
For 2nd code, look at the 2 formula container highlighted in green. Both of them process the array at once without iterating the array. In this case the “asInt16(x)” and “A*LSB_Volt” been process faster than iterating each element of the array. VEE optimized the array operation like this. So, lets calculate how many operation for the 2nd code:
1. [B+1]
2. bitShift(A[B+1],8)
3. bitShift(A[B+1],8) + A[B]
Total = 3 * 1M + 1 (asInt16(x)) + 1 (A*LSB_Volt) = 3000002
From the comparison of the number of execution, 5M : 3M. The results tally with the profiling results, because the ratio 5M:3M ~= 25.84:13.18
To further improve on the performance, I try to avoid using the For Range. I studied how the data were manapulated. Then I found out I can use the "From String" container to slice the 1D array into 2D array. Then the rest of the operation can be done by processing the arrays as a whole. By using this approach, I manage to optimize the processing time to 3~4 seconds.
Figure3: Optimized Code
Figure4: Profiling result
0 comments:
Post a Comment