This is a creation in Article, where the information may have evolved or changed.
1: In Go language, first look at the performance analysis of creating slice, we know slice has append this function
T: = time. Now () m: = []string{} for I: = 0; i < 1000000; i++ { m = append (M, StrConv. Itoa (i)) }fmt. Println (time. Now (). Sub (t))
You can see that 1000000 items were added and took time 368.0211ms.
We can also define a fixed-length silice by ourselves:
T: = time. Now () m:=make ([]string,1000000) for I: = 0; i < 1000000; i++ { m[i]=strconv. Itoa (i)}fmt. Println (time. Now (). Sub (t))
It takes time to 52.003ms, feels good, and improves performance by 6-7 times.
Append is to add the original slice, and then give a new slice, is the constant creation of slice, and belong to the interface type, so it takes a lot of time.
2: Concatenate the slice into a string.
The first method uses strings directly. Join to see the performance.
T: = time. Now () s:=strings. Join (M, "") FMT. Println (time. Now (). Sub (t)) FMT. Println (Len (s))
Cost 12.0007ms. is still relatively fast.
The second method, the direct traversal.
s:= "" t: = time. Now () for I: = 0; i < 1000000; i++ { s=s+m[i] } fmt. Println (time. Now (). Sub (t)) FMT. Println (Len (s))
Unfortunately, it took nearly 170 seconds to see a 1200 times-fold difference in performance. Remember that the string is very large, do not use s=s+a[i] this way, because the constant creation of memory space to add, not only the program is very card, greatly consumes the resources, but also very slow.
____________________________________________________________
In Python:
Import time# First Way, appenda=[]t=time.time () for I in Range (1000000): a.append (str (i)) print (Time.time ()-T) Elapsed time: 0.34202003479003906 seconds # Second way, List derivation t=time.time () a=[str (i) for I in Range (1000000)]print (Time.time ()-T) Elapsed time: 0.25701379776000977 seconds The Third way, the first application space, then the assignment takes time 0.3130180835723877 seconds t=time.time () a = [1] * 1000000for i in range (1000000): a[i]=str (i) Print (Time.time ()-T)
As you can see, in general Python is still fairly smooth.
Look at string connection mode 1
T=time.time () s= "". Join (a) print (Time.time ()-T)
10W items take 0.015001058578491211 seconds =15 milliseconds, similar to the go language.
Look at string connection mode 2
T=time.time () s= "" for item in A: s=s+itemprint (Time.time ()-t) print (len (s))
It takes 1.64809393882751 seconds, the same way 100 times times faster than the go language, but it's also very slow, so it's not recommended to use this thing. In fact, it can be guessed that some people use a compiled language to write programs that can execute slower than some scripting languages. This is the experience that programmers usually accumulate.
The conclusion is that this is not the way to use s=s+m[i] when traversing the list connection string, especially when the list and slice elements are sufficient, and this principle works in whatever language.