String String and StringBuffer discovery of string concatenation

In future work, basically will encounter some operations associated with the date or time, it also offers a number of java classes, it can easily developers to manipulate dates and times. Demonstrate it through code

Create a new package called dete inside to a class

Ideas (refer to):

First, get the number of milliseconds, one second is equal to one thousand milliseconds, in this class System inside, there is a method that it returns a value of type lang, lang represents this type is from January 1, 1970 00 00 minutes 00 seconds 000 milliseconds from the beginning of this period the number of milliseconds to now flies, after printing execution can see that this is a growing number. If string splicing affects some performance, first to a string, annotation and presentation string stringbuffer time spent stitching 1000, again a stringbuffer teacher probably wrote the length and wrote 1000. And then to a discharge cycle, int i = 0 i <1000 ++, firstly s = i +; loop stitching want to splice 1000 1000 acquires the number of milliseconds before splicing, obtaining the number of milliseconds before splicing, splicing after also get the number of milliseconds to get the number of milliseconds after stitching ,, copy betore changed after, let them subtraction, both made with reduced number of milliseconds spent stitching is performed, the fastest is 7 ms, each time there is changes, determined in accordance with the computer running, the teacher commented after a part of the string before splicing, is the fastest one millisecond, both poor 6 ms. Future splicing must pay attention not to use stringbefore string perform a channel stitching.

Here is the code cases:

public class Dateresto{

public static void main(string[] args) {

// From January 1, 1970 00 hours 00 minutes 00 seconds 000 milliseconds to now the number of milliseconds

long now = System. currentrimeMillis() ;

System. out. println (now) ;

// String class presentations and StringBuf fer time spent stitching 1000

String 5 =

StringBuffer sb = new stringBuffer (1000) ;

// Get the number of milliseconds before splicing

long before = System. currentTimeMillis() ;

for(int i=0; i<1000; i++) {

s+=i;

/ / Get the number of milliseconds after splicing

long after = System. currentTimeMillis() ;

System. out. printin(arter一before) ;

}

}

 

Now the weather hot and cold, keep warm, do not have a cold, remember to keep the mood relaxed, after all, a good mood was high work efficiency of thing.

Guess you like

Origin www.cnblogs.com/JiXianSen/p/12413291.html