public class Mutiply {
public static void main(String[] args) {
BigInteger multiplier = new BigInteger("-673589893565890368590578967285490682958485023495834902583490589345798583495723487563482756347856673589893565890368590578967285490682958485023495834902583490589345798583495723487563482756347856");
BigInteger multiplicand = new BigInteger("-859085903425878805823409584039269057908349673285902345849035890878809586908504397562358476278584376328590385689457043257834927534095734956347853485");
long st1 = System.currentTimeMillis();
for(int index=0;index<1000;index++){
multiply(multiplier, multiplicand);
}
System.out.println(System.currentTimeMillis()-st1);
long st2 = System.currentTimeMillis();
for(int index=0;index<1000;index++){
multiplier.multiply(multiplicand);
}
System.out.println(System.currentTimeMillis()-st2);
}
public static String multiply(BigInteger multiplier, BigInteger multiplicand){
boolean isResultNegative = false;
if(multiplier.signum()==0 || multiplicand.signum()==0){
return "0";
}
if(multiplier.signum()==-1 || multiplicand.signum()==-1){
if(!(multiplier.signum()==-1 && multiplicand.signum()==-1)){
isResultNegative = true;
}
}
return (isResultNegative?"-" : "") + multiply(multiplier.toString().replace("-", ""), multiplicand.toString().replace("-", ""));
}
public static String multiply(String multiplier, String multiplicand){
if(multiplier.length()>multiplicand.length()){
String temp = multiplier;
multiplier = multiplicand;
multiplicand = temp;
}
int length = multiplicand.length()+multiplier.length();
char[] result = new char[length];
for(int indexOfResult=0;indexOfResult<result.length;indexOfResult++){
result[indexOfResult] = '0';
}
String[] tempResults = new String[multiplier.length()];
for(int indexOfMultiplier=0;indexOfMultiplier<multiplier.length();indexOfMultiplier++){
char[] tempResult = new char[length];
for(int indexOftempResult=0;indexOftempResult<tempResult.length;indexOftempResult++){
tempResult[indexOftempResult] = '0';
}
int singleMultiplier = Integer.valueOf(String.valueOf(multiplier.charAt(multiplier.length()-1-indexOfMultiplier)));
if(singleMultiplier!=0){
int singleStep = 0;
for(int indexOfmultiplicand=0;indexOfmultiplicand<multiplicand.length();indexOfmultiplicand++){
int singleMultiplicand = Integer.valueOf(String.valueOf(multiplicand.charAt(multiplicand.length()-1-indexOfmultiplicand)));
int singleResult = singleStep + singleMultiplier*singleMultiplicand;
singleStep = singleResult/10;
tempResult[tempResult.length-indexOfMultiplier-indexOfmultiplicand-1] = (""+singleResult%10).charAt(0);
}
tempResult[tempResult.length-indexOfMultiplier-multiplicand.length()-1] = ("" + singleStep).charAt(0);
}
tempResults[indexOfMultiplier] = String.valueOf(tempResult);
}
int step = 0;
for(int indexOfResult=0; indexOfResult<length; indexOfResult++){
int multiResult = step;
for(int indexOfTempResults=0;indexOfTempResults<tempResults.length;indexOfTempResults++){
multiResult += Integer.valueOf(String.valueOf(tempResults[indexOfTempResults].charAt(length-1-indexOfResult)));
}
step = multiResult/10;
result[result.length-indexOfResult-1] = (""+multiResult%10).charAt(0);
}
return String.valueOf(result);
}
}
结果对比:
578671582252594476786199322413773832540849019479781209035507587576779550181736165461400001640216752920490028498890769204733283108280630943510991915921171610703486725119777750465131309033130699124992461394973593046143133767470798477438073390294712136103115909945569596014299669909031490482950743553619069179063137722898136350387343581878160
578671582252594476786199322413773832540849019479781209035507587576779550181736165461400001640216752920490028498890769204733283108280630943510991915921171610703486725119777750465131309033130699124992461394973593046143133767470798477438073390294712136103115909945569596014299669909031490482950743553619069179063137722898136350387343581878160
虽然结果一致,但由于本人实现方式不算优,性能比JAVA默认的BigInteger实现整数相乘性能差很多,尤其是在计算遍数多的情况下。不是一个数量级的。