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.

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;
*(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=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);
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



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