Java and C speed performance comparison-Matrix Multiplication

Source: Internet
Author: User

Java code:

 

Public class matrice {

Public static void main (string [] ARGs)
{
Matrice MA = new matrice ();

Long mseconds = 0;
Int times = 100;
Mseconds = ma. timeneed (times );
System. Out. println ("100*100 matrice:" + mseconds + "Ms ");

Times = 200;
Mseconds = ma. timeneed (times );
System. Out. println ("200*200 matrice:" + mseconds + "Ms ");

Times = 500;
Mseconds = ma. timeneed (times );
System. Out. println ("500*500 matrice:" + mseconds + "Ms ");

Times = 5000;
Mseconds = ma. timeneed (times );
System. Out. println ("5000*5000 matrice:" + mseconds + "Ms ");

Times = 8000;
Mseconds = ma. timeneed (times );
System. Out. println ("8000*8000 matrice:" + mseconds + "Ms ");

}
Public
Long timeneed (int n)
{
Int [] [] A = new int [N] [N];
Int [] [] B = new int [N] [N];
Int [] [] C = new int [N] [N];

For (INT I = 0; I <n; I ++ ){
For (Int J = 0; j <n; j ++)
{
A [I] [J] = N;
B [I] [J] = N;
C [I] [J] = N;
}
}


Long start = system. currenttimemillis ();

For (INT I = 0; I <n; I ++)
{
For (Int J = 0; j <n; j ++)
{
C [I] [J] + = A [I] [J] * A [I] [J];
}
}
Long end = system. currenttimemillis ();
Return (end-Start );
}
}

Output:

 

100*100 matrice: 1 MS
200*200 matrice: 1 MS
500*500 matrice: 2 MS
5000*5000 matrice: 202 MS
8000*8000 matrice: 524 MS

 

Note that the heap memory of the Java Virtual Machine is online and can be set by yourself.

The multiplication of arrays of 8000*8000 has basically reached the default upper limit. If it is larger, you need to set it by yourself.

 

 

 

C code (V. S. 2008 environment ):

# Include "stdafx. H"
# Include <stdlib. h>
# Include <time. h>
# Include <iostream>
# Include <cstdlib>
Using namespace STD;

Long timeneed (int n)
{
Int **;
Int ** B;
Int ** C;

A = (INT **) malloc (sizeof (int *) * n );
B = (INT **) malloc (sizeof (int *) * n );
C = (INT **) malloc (sizeof (int *) * n );
For (INT I = 0; I <n; I ++)
{
A [I] = (int *) malloc (sizeof (INT) * n );
B [I] = (int *) malloc (sizeof (INT) * n );
C [I] = (int *) malloc (sizeof (INT) * n );

}



Long start = clock ();

For (INT I = 0; I <n; I ++)
{
For (Int J = 0; j <n; j ++)
{
C [I] [J] + = A [I] [J] * A [I] [J];
}
}
Long end = clock ();
Return (end-Start );
}

Void main ()
{

Long mseconds = 0;
Int times = 100;
Mseconds = timeneed (times );
Cout <"100*100 matrice:" <mseconds <"Ms" <"/N ";

Times = 200;
Mseconds = timeneed (times );
Cout <"200*200 matrice:" <mseconds <"Ms" <"/N ";

Times = 500;
Mseconds = timeneed (times );
Cout <"500*500 matrice:" <mseconds <"Ms" <"/N ";

Times = 5000;
Mseconds = timeneed (times );
Cout <"5000*5000 matrice:" <mseconds <"Ms" <"/N ";

Times = 8000;
Mseconds = timeneed (times );
Cout <"8000*8000 matrice:" <mseconds <"Ms" <"/N ";

System ("pause ");

}

 

Output:

100*100 matrice: 0 MS
200*200 matrice: 1 MS
500*500 matrice: 4 MS
5000*5000 matrice: 354 MS
8000*8000 matrice: 914 MS

 

Run the Java code again after running the C program

The result is as follows:

100*100 matrice: 0 MS
200*200 matrice: 0 MS
500*500 matrice: 2 MS
5000*5000 matrice: 249 MS
8000*8000 matrice: 515 MS

 

You can also paste your running results to see where it is!

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.