This is a creation in Article, where the information may have evolved or changed.
For example, we add from 1 to 100, we know 101*50=5050 as a child, this is the use of algorithms, but we do not discuss the algorithm here, just to verify the computing power of the computer, in the go language, we design the traversal.
Func Main () { ts: = time. Now (). Unixnano () h: = 1000000000 sum: = 0 for i: = 0; I <= h; i++ { sum = sum + i } fmt. Println (sum) FMT. Print ("Time MS:") FMT. Println (time. Now (). Unixnano ()-TS)/1000000)}
Calculation Result:
500000000500000000
Time MS: 289
This single-threaded calculation, wasting the computer's multicore performance, and go concurrency is the use of multi-core, so we open up a few threads, each thread calculates a paragraph, for example, we want to calculate 1 to 100 equals how much, we can open 10 threads, respectively calculate 1-10,10-20 and so on.
Func Count1 (start int, end int, ch Chan int) { var cccc int for J: = start; J < End; J + + { CCCC = CCCC + j< c3/>} Ch <-cccc}func Main () { ts: = time. Now (). Unixnano () h: = 1000000000 sum: = 0 ch: = Make (chan int, n) numlength: = Cap (CH) for I: = 0; i < Numlength; i++ { num: = H/numlength go Count1 (num*i, Num*i+num, ch) } for I: = 0; i < numlength; i++ { Select {Case msg: = <-ch: sum = sum + msg } } fmt. PRINTLN (sum + h) FMT. Print ("Time MS:") FMT. Println (time. Now (). Unixnano ()-TS)/1000000)}
Calculation Result:
500000000500000000
Time MS: 75
Can be compared, the efficiency increased 4-5 times, so we can infer that I use the computer in the Internet Café CPU core between 4-8 cores, the CPU core number is generally 2 of the second-party calculation, and the system will not be the CPU's computing power allocated to my program, so the 8 core is the most reliable, (own no computer, Also can not afford, here on the Internet 6 yuan one hours, rushed 100 to send 100, belong to the esports zone, but also acceptable, the other interval 4 yuan an hour), in fact, and did not fully utilize the 8 core full performance, because this is the system reason.
Look at the python.
Write a single-threaded case: This time 100000000, note that this is a 0 less than the number of Go language calculations above.
Import timesum=0ts=time.time () for I in Range (100000000): sum=sum+iprint (' Spend time: (seconds) ' + str (time.time ()- TS))
Settlement results:
Time taken: (seconds) 10.329591035842896
By contrast, Python's traversal calculation is a bit weak because it is a script that generates an object every time, so the time it takes to create an object is too high.
To write multi-threaded, segmented computing:
Import Threadingimport timets=time.time () Mutex = threading. Lock () sumlist=[]def count (start,end): count=0 for i in range (start,end): count = count + I Mutex.acquire () sumlist.append (int (count)) mutex.release () li = []h=100000000slicelen=10for i in range ( Slicelen): num = int (h/slicelen) th = Threading. Thread (Target=count, args= (num*i,num*i+num)) Th.start () li.append (TH) for i in Li: i.join () print (sum (sumlist) +h) print (' Spend time: (seconds) ' + str (time.time ()-ts))
Time taken: (seconds) 6.120349884033203
You can see that Python does not really take full advantage of multicore, and because of the Gil's limitations, it is not really using multicore to compute, but because of the additional work that is being done to reduce the burden on the main CPU, performance is much higher than a single thread.