Java Byte. toString method and String. ValueOf (Byte) Efficiency Comparison, valueof and tostring
Int times = 10000000; Byte [] li = new Byte [times]; for (int I = 0; I <times; I ++) {li [I] = (byte) i;} long timeA = System. currentTimeMillis (); for (int I = 0; I <times; I ++) {li [I]. toString ();} long timeB = System. currentTimeMillis (); for (int I = 0; I <times; I ++) {String. valueOf (li [I]);} long timeC = System. currentTimeMillis (); System. out. println ("A method time consumed:" + (timeB-timeA); System. out. println ("B method time consumed:" + (timeC-timeB); if (timeB-timeA> timeC-timeB) {System. out. println ("good method B");} else {System. out. println ("good method ");}
RunResult:
Method A time consumption: 1303
Method B time consumption: 239
Good Method B
String.valueOf(li[i]);
Why does li [I] become an Object? Shouldn't I call String. valueOf (Byte )? Please advise