High Level Vs Low Level
In high level language, we don’t need to think about memory allocation. For example, to create dynamic array, we don’t need to initialize the array length. But, all would be different if we move to low level language like C where required strong identifier in variable declaration. Then you face with a problem that required you to check unregistered value on certain array. In higher language, we just need to address new key without need to think about memory allocation. But in low level language, there is no way to do new array key creation as long as we have fixed length array.
Study Case: Fibonacci Series
Problem to find n-th value form fibonacci series solved by simple axiom:
Fn=F(n-1)+F(n-2) for n≥2. In algorithm implementation, we will solve that problem using Top-Down paradigm that has optimal solution
O(n). This paradigm looked the problem from top to bottom level.
Algorithm
In the real world, we calculate the n-th fibonacci series by write every single number on the paper. So, our algorithm to find n-th value just like read the existing sequence, then write the calculation result. Let’s said we already have a fibonacci series; (0, 1, 1) and this series are true as long as last index equal 1+0. To solve any n-th value, we only need to “grow up” the series. For example; to find n=5, we just need to do three times calculation and write every result behind.
1
2
3
4
5
6
7
| var m = {0, 1, 1}
Function FiboTopDown(n)
if key n not exist in array m then
m[n] = FiboTopDown(n-1) + FiboTopDown(n-2)
else
return m[n]
end
|
Now, time to implement this algorithm into a real code.
High Level Solution
The big advantage using high level language is logic priority. It’s means we don’t need to think how machine execute our data or input. We don’t need to think about memory allocation or data type. In this section, i choose using Python to code the algorithm above. The code should be
1
2
3
4
5
6
7
| fiboDict = {0:0, 1:1, 1:1};
def fiboTopDown(n):
if (n not in fiboDict.keys()):
fiboDict[n] = fiboTopDown(n-1)+fiboTopDown(n-2)
return fiboDict[n]
else:
return fiboDict[n]
|
Because there was no term called array in python data structure doctrine, i used dictionary which represent key – value pair.
Low Level Solution
As I said before, the low-level language like C has strong “machine” style. Although the algorithm logically true, but be the wrong solution by the language doctrine. In low-level language like C, the way to just create a new key on array is very bad practice. Although this is possible, but the implementation would be not elegant. Then we must modify our algorithm little bit. To find n-th value of fibonacci series, means maximum length of our array equal to n. Then create an array with n allocation and all values are zero. Fill value of index 0 to 3 with 0, 1, 1. Then do value checking of certain key; if it has value equal 0, change its value with the calculation result. The code should be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| #include <stdio.h>
unsigned int *fiboArr;
main()
{
unsigned int n;
printf(“Insert n-th to find:");
scanf("%d", &n);
fiboArr = allocateArray(n+1);
fiboArr[0] = 0;
fiboArr[1] = 1;
fiboArr[2] = 1;
printf("%d", fiboTopDown(n));
}
unsigned int fiboTopDown(int n)
{
if (fiboArr[n] == 0) {
fiboArr[n] = fiboTopDown(n-1) + fiboTopDown(n-2);
} else {
return fiboArr[n];
}
}
unsigned int * allocateArray(unsigned int len)
{
unsigned int *arr = (unsigned int*) calloc( len, sizeof(unsigned int) );
return arr;
}
|
Conclusion
If time is mine, the speed of program building should more important than how fast and “beauty” when it have done. We found this orientation on software developer world where there always pressure around direction. This condition does not matter by most people whom can invest very big cost to keep software running, create the hardware requirements bubbling. This implication not worst at all, because when hardware becomes cheaper and the rise of ‘human-friendly’ programming language meet, stimulate to increase creativity and developing innovative. We can use framework to boost production, API to simplify our algorithm and libraries to safe code lines. But, there was a point where any software needs to optimize their processing and efficiency. When the problem grows up, we need to increase execution time and reduce memory consumption. For example, your database has been bigger and data processing take longer time. Then you may look up your query to optimize execution time and change database engine to save installation costs. This point called as optimization where the goal changes from building speed to solution efficiency.
0 comments :
Post a Comment