Testing multi-core multithreading in the Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

concurrency (concurrency) and parallel (parallelism) are different. In a single CPU core, the thread through the time slice or give up control to achieve task switching, to "simultaneously" transport ⾏ multiple Tasks ⺫, which is called concurrency. But in fact, only one task is executed at any moment, and other tasks are queued by some sort of algorithm. Multi-core CPUs allow the "multiple threads" in the same ⼀ process to be transported in real time, without queuing between them (the queue can still occur because the number of threads may exceed the number of CPU cores, there are other processes, and so on.) This is an ideal condition), which is parallel. In addition to multicore, parallel computing may also be a deployment run on more than one machine.

Package Mainimport ("FMT""Runtime") func Test (C ChanBOOLNint) {x: =0 forI: =0; I <1000000000; i++ {x + = I}PRINTLN (n, x)ifn = =9{C <-true}}func Main () {runtime. Gomaxprocs (1)//Set the number of cores in the CPU to achieve high concurrencyc: = Make (chanBOOL) forI: =0; I <Ten; i++ {Go Test (c, i)}<-cfmt. Println ("main OK")}

The test results are as follows:

Using the results of one core

[' Go run Sendmail.go ' | done:24.6124077s] Main OK0 4999999995000000001 4999999995000000002 4999999995000000003 4999999995000000004 4999999995000000005 4999999995000000006 4999999995000000007 4999999995000000008) 4999999995000000009 499999999500000000

Setting 8-Core Results

[' Go run Sendmail.go ' | done:6.1073493s] Main OK0 4999999995000000001 4999999995000000006 4999999995000000008 4999999995000000003 4999999995000000009 499999999500000000

It can be seen that the execution time with 1 cores is 24 seconds, while the execution time of 8 cores is 6 seconds. The running time is greatly reduced. There is no lock function, so the order of their display is random. So there are some problems with the loss of print results

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.