Performance difference of same program compiled differently on android, Why (I don’t know)

While playing around android, I was bench-marking my device using a simple 1000×1000 matrix multiplication program which is also listed below.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int n=1000;
int main(){
int i,j,k,sum;
int no_of_bytes=n * n * sizeof(int);
int *A = (int *)malloc(no_of_bytes);
int *B = (int *)malloc(no_of_bytes);
int *C = (int *)malloc(no_of_bytes);
clock_t begin, end;
double time_spent;
srand(time(NULL));
for(i=0;i<n;i++){
for(j=0;j<n;j++){
*(C + i*n + j) = 0;
*(A + i*n + j) =(int)(rand()/500000);
*(B + i*n + j) =(int)(rand()/500000);
}
}
begin = clock();
for(i=0;i<n;i++){ //row of first matrix
for(j=0;j<n;j++){ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+( (*(A + i*n + j)) * (*(B + i*n + j)) );
(*(C + i*n + j))=sum;
}
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf ("Time taken to compute the matrix is %.2f\n", time_spent);
free(A);
free(B);
free(C);
return 0;
}

Now I compiled this program using two different methods.

Method 1: 

The program is added into the android source tree and compiled along with compilation of android from source code by  the android build process. Which generates a dynamically linked executable. This will off-course use the bionic c library. The output from the file command is something like this:

 ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), stripped 

 

Method 2: 

In this compilation method, the program is compiled using “arm-linux-gnueabi-gcc” with a -static flag generating a statically linked executable. The static linking was performed because “arm-linux-gnueabi-gcc” would normally use the glibc, which is not available in android.  The output from the file command is something like this:

ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=b3f88119c36f1ddb318eb0fbe8ade60a792b1562, not stripped

 

Result: 

Now when I executed both these programs on a same device (GT-i9100g), I got a substantial difference and I don’t know why. If any body have any clue please mention it in the comments.

Execution Time
Method 1 0.03  seconds
Method 2 38.56 seconds

 

Advertisements

2 thoughts on “Performance difference of same program compiled differently on android, Why (I don’t know)

  1. My guess would be, when built statically compiler will be conservative and compile the program for most basic architecture i.e v4 (ARM 7TDMI)
    And when built dynamically it would pick up system libraries optimized for the system at runtime. which will be in your case v7 (Cortex-A9 with NEON).
    One way to test this hypothesis is to build your application statically by specifying -mcpu=cortex-a9

    Even with this the statically linked one is slow then, try getting the libs from target and link statically to see dis assembly.

    Like

  2. @giridhart you are right I figured it out later. Google has done some crazy optimization for their BIONIC library so when dynamically linked it utilizes BIONIC which results in better performance

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s