Last active
September 10, 2023 12:59
-
-
Save Mark1002/86efffdc7362140ece80288424acaffa to your computer and use it in GitHub Desktop.
python and go concurrency comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import time | |
import random | |
async def task(n: int): | |
await asyncio.sleep(random.randint(1, 3)) | |
print(f"task{n} finish") | |
async def main(): | |
tasks = [task(i) for i in range(4000)] | |
for future in asyncio.as_completed(tasks): | |
await future | |
if __name__ == '__main__': | |
start_time = time.perf_counter() | |
asyncio.run(main()) | |
end_time = time.perf_counter() | |
print(f'Took {end_time - start_time:.4f} seconds') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"sync" | |
"time" | |
) | |
func main() { | |
startTime := time.Now() | |
var wg sync.WaitGroup | |
taskNum := 4000 | |
wg.Add(taskNum) | |
for i := 0; i < taskNum; i++ { | |
// can do both IO/CPU bound task | |
go func(i int) { | |
randSec := time.Duration(rand.Intn(3)+1) * time.Second | |
time.Sleep(randSec) | |
fmt.Printf("task%d finish\n", i) | |
defer wg.Done() | |
}(i) | |
} | |
wg.Wait() | |
// Calculate the elapsed time in seconds | |
elapsed := time.Since(startTime).Seconds() | |
fmt.Printf("Took: %.5f seconds\n", elapsed) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import random | |
import multiprocessing | |
def task(n): | |
time.sleep(random.randint(1, 3)) | |
print(f"task{n} finish") | |
def main(): | |
processses = [ | |
multiprocessing.Process(target=task, kwargs={"n": i}) | |
for i in range(4000) | |
] | |
for processs in processses: | |
processs.start() | |
for processs in processses: | |
processs.join() | |
if __name__ == "__main__": | |
start_time = time.perf_counter() | |
main() | |
end_time = time.perf_counter() | |
print(f'Took {end_time - start_time:.4f} seconds') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import random | |
import threading | |
def task(n): | |
time.sleep(random.randint(1, 3)) | |
print(f"task{n} finish") | |
def main(): | |
threads = [ | |
threading.Thread(target=task, kwargs={"n": i}) | |
for i in range(4000) | |
] | |
for thread in threads: | |
thread.start() | |
for thread in threads: | |
thread.join() | |
if __name__ == "__main__": | |
start_time = time.perf_counter() | |
main() | |
end_time = time.perf_counter() | |
print(f'Took {end_time - start_time:.4f} seconds') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment