Native code performance

As we already know, native code can run faster with better processing speed. This can be further optimized for a specific CPU architecture. The main reason behind this performance boost is the use of pointers in memory operations. However, it depends on the developer and the coding style.

Let's look at a simple example where we can have a better understanding of performance gain in native language.

Consider this Java code:

int[] testArray = new int[1000];
for ( int i = 0; i < 1000; ++ i)
{
  testArray[i] = i;
}

In this case, the address of 1000 fields in the array is handled by JVM (DVM in the case of an Android Dalvik system). So, the interpreter parses to the ith position and performs an assignment operation each time, which takes a lot of time.

Now, let's implement the same functionality using native C/C++ language and use pointers:

int testArray[1000];
int *ptrArray = testArray;
for ( int i = 0; i < 1000; ++ i)
{
  *ptrArray = i;
  ptrArray += i * sizeof(int);
}

In this example, the interpreter does not need to parse to the target memory location. The address of the location is pointed out by ptrArray. Hence, the value can be directly assigned to the memory location.

Especially for multi-dimensional arrays, a significant performance gain can be observed in the case of properly written native code for the same functionality. Other important use of native code is binary data processing and image processing, where a huge amount of data is processed at a time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.35.255