Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
January 24th, 2013, 10:08 AM
 kungfupanda
Registered User

Join Date: Jan 2013
Posts: 1
Time spent in forums: 26 m 16 sec
Reputation Power: 0
Optimize this code

import java.util.*;
import java.math.*;
class M
{

public static void main(String args[])
{
final int size=3;
Scanner s=new Scanner(System.in);
int a1[]=new int[size];
int a2[]=new int[size];
int i,num1,num2,sum;
i=0;
num1=0;
num2=0;
sum=0;
System.out.println("Enter elements for 1st array \n");
for(i=0;i<size;++i)
a1[i]=s.nextInt(); // scanf("%d",&a1[i]);
System.out.println("Enter elements for 2nd array \n");
for(i=0;i<size;++i)
a2[i]=s.nextInt(); // scanf("%d",&a2[i]);
num1 = convert(a1);
num2 = convert(a2);
sum = num1 + num2;
System.out.println("Sum of arrary \n"+sum);
}
static int convert(int arr[])
{
int i =0, num=0;
final int size=3;
try{
for(i = 0;;++i)
{
num += arr[i] * Math.pow(10,i);
System.out.println("Sum of array a[size] \n"+(int)Math.pow(10,i)+""+arr[i]);
}

}catch(Exception e){}
return num;
}
}
somebody optimize this code right now it is taking 246mb

#2
January 24th, 2013, 11:33 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 3,840
Time spent in forums: 2 Months 3 Weeks 2 Days 21 h 15 m 20 sec
Reputation Power: 1774
> somebody optimize this code right now it is taking 246mb
Which real operating system are you using?
Which JavaVM are you using?
How did you arrive at 246Mb?

For me, your program uses about 15Mb (it uses 14Mb on just "hello world")
\$ java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~11.10.1)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Optimize this code