承上篇:[JAVA]數值比大小之double[]
以下呈現幾種BigDecimal array比大小的寫法和速度
內容包含計算array中的加總sum,取最大max,取最小min,平均avg
速度是double3 > double2
array數值10萬筆,decimal2執行91 ms,decimal3執行32 ms
decimal2還有偷懶不算加總和平均,速度仍然筆decimal3還要慢
init array ----------------------
count = 100,000
Test2 ----------------------
sum = 0 , max = 1000.996076938839223657851107418537139892578125 , min = 1.0000248972125138635647090268321335315704345703125 , avg = 0
Time: 91 ms
Test3 ----------------------
sum = 50051208.58220991677612499159977232920937240123748779296875 , max = 1000.996076938839223657851107418537139892578125 , min = 1.0000248972125138635647090268321335315704345703125 , avg = 500.5120858220991677612499159977232920937240123748779296875
Time: 32 ms
程式如下 :
import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Arrays; import java.util.Collections; @Slf4j public class DecimalArray { public static void main(String[] args) { System.out.println("init array ----------------------"); int count = 100000;//可以指定要多少數量去比大小 DecimalFormat df = new DecimalFormat("#,###"); System.out.println("count = " + df.format(count)); BigDecimal[] init_BigDecimal = new BigDecimal[count]; for(int i = 0 ;i < count; i++){ init_BigDecimal[i] = new BigDecimal(Math.random() *1000+1); } //array 0:max,1:min,2:avg BigDecimal[] result_Decimal = new BigDecimal[3]; result_Decimal = decimal2( init_BigDecimal ); result_Decimal = decimal3( init_BigDecimal ); } public static BigDecimal[] decimal2(BigDecimal[] init ) { System.out.println("Test2 ----------------------"); Long start = System.currentTimeMillis(); BigDecimal sum = new BigDecimal(0) ; BigDecimal max = new BigDecimal(0) ; BigDecimal min = new BigDecimal(0) ; BigDecimal avg = new BigDecimal(0) ; BigDecimal[] result = new BigDecimal[3]; int length = init.length; Arrays.sort(init); max = init[length-1]; min = init[0]; System.out.println(String.format("sum = %s , max = %s , min = %s , avg = %s",sum,max,min,avg)); Long end = System.currentTimeMillis(); System.out.println("Time: " + (end - start) + " ms"); result[0] = max; result[1] = min; result[2] = avg; return result; } public static BigDecimal[] decimal3(BigDecimal[] init ) { System.out.println("Test3 ----------------------"); Long start = System.currentTimeMillis(); BigDecimal sum = new BigDecimal(0) ; BigDecimal max = new BigDecimal(0) ; BigDecimal min = new BigDecimal(0) ; BigDecimal avg = new BigDecimal(0) ; BigDecimal[] result = new BigDecimal[3]; int length = init.length; Arrays.sort(init); for(int i = 0 ; i < length ; i++){ if(i == 0){ min = init[i]; } sum = sum.add(init[i]); //max < init[i] if(max.compareTo(init[i]) == -1){ max = init[i]; } //min > init[i] if(min.compareTo(init[i]) == 1 ){ min = init[i]; } } avg = sum.divide(new BigDecimal(length)); System.out.println(String.format("sum = %s , max = %s , min = %s , avg = %s",sum,max,min,avg)); Long end = System.currentTimeMillis(); System.out.println("Time: " + (end - start) + " ms"); result[0] = max; result[1] = min; result[2] = avg; return result; } }
留言列表