For algorithms that work with datasets, there are two things that significantly change performance between languages, such as Java, and C:
check for array binding. Java checks every access, bmap [i] and confirms that I am within the array. If the code tries to access outside, you will get a useful exception. C and C ++ do not check anything and trust your code. The best answer to accessing overseas access is a page error. A more likely outcome is "unexpected behavior."
pointers. You can significantly reduce operations with pointers.
Take this innocent example of a generic filter (similar to blur, but 1D):
for{i=0; i<ndata-ncoef; ++i) { z[i] = 0; for{k=0; k<ncoef; ++k) { z[i] += c[k] * d[i+k]; } }
When you access an element of an array, coef [k]:
- coef array load address in register
- load value k in register
- sum them up
- go get memory at this address
Each of these array calls can be improved since you know that indexes are sequential. The compiler or JIT may know that the indices are sequential, so they cannot be fully optimized (although they keep trying).
In C ++, you write code something like this:
int d[10000]; int z[10000]; int coef[10]; int* zptr; int* dptr; int* cptr; dptr = &(d[0]);
When you first do something like this (and succeed in using it correctly), you will be surprised how much faster it can be. All array address calculations to extract the index and sum the index and base address are replaced by an increment statement.
For 2D array operations, such as image blur, the data of innocent codes [r, c] are associated with two values, multiplication and sum. Thus, using 2D arrays, the advantages of pointers can remove multiplication operations.
Thus, the language can really reduce the operations that the processor must perform. The cost is that C ++ code is terrible for reading and debugging. Errors in pointers and buffer overflows are food for hackers. But when it comes to raw chopping algorithms, speed improvement is too tempting to ignore.
jdr5ca
source share