Last active
March 10, 2021 00:59
-
-
Save sj82516/df8e34f1ba7952817d3da2607c3eda35 to your computer and use it in GitHub Desktop.
arraySum
This file contains 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" | |
"math/rand" | |
"sync" | |
"sync/atomic" | |
"time" | |
) | |
func main(){ | |
arr := [1000][1000]int{} | |
for i := range arr { | |
for j:= range arr[i]{ | |
arr[i][j] = rand.Intn(100) | |
} | |
} | |
cores := 4 | |
var total int64 = 0 | |
start := time.Now() | |
wg := sync.WaitGroup{} | |
wg.Add(4) | |
for i:=0; i<cores;i++{ | |
step := int(math.Floor(float64(len(arr[0]) / cores))) | |
rowStart := step * i | |
rowEnd := step * (i+1) | |
go rangeSum(rowStart, rowEnd, &arr, &total, &wg) | |
} | |
wg.Wait() | |
fmt.Printf("Concurrency Cacl in %v, with total %v\n", time.Since(start), total) | |
start = time.Now() | |
total = 0 | |
for i := range arr { | |
for j:= range arr[i]{ | |
total = total + int64(arr[i][j]) | |
} | |
} | |
fmt.Printf("Sync Cacl in %v, with total %v\n", time.Since(start), total) | |
} | |
func rangeSum (rowStart int, rowEnd int, arr *[1000][1000]int, total *int64, wg *sync.WaitGroup){ | |
sum := 0 | |
for i:= rowStart; i<rowEnd; i++{ | |
for j:=0; j<len(arr[i]); j++{ | |
sum = sum + arr[i][j] | |
} | |
} | |
atomic.AddInt64( | |
total, | |
int64(sum), | |
) | |
wg.Done() | |
} |
This file contains 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
const { parentPort, Worker, isMainThread } = require('worker_threads'); | |
const ROW_AMOUNT = 3000; | |
if (isMainThread) { | |
const arr = []; | |
for(let i=0; i< ROW_AMOUNT; i++){ | |
arr[i] = [] | |
for(let j=0; j < ROW_AMOUNT; j++) { | |
arr[i].push(Math.floor(Math.random() * 10)); | |
} | |
} | |
const cores = 4; | |
let total = 0; | |
let finished = 0; | |
let start = new Date().getTime(); | |
for(let i=0; i < cores; i++){ | |
const step = Math.floor(ROW_AMOUNT / cores); | |
const rowStart = i * step; | |
const rowEnd = (i+1) * step; | |
const worker = new Worker(__filename); | |
worker.once('message', (sum) => { | |
finished++; | |
total += sum; | |
if(finished === cores){ | |
console.log(`Concurrency Cacl in ${new Date().getTime() - start}, with total ${total}`) | |
start = new Date().getTime(); | |
total = 0; | |
for(let i=0; i<ROW_AMOUNT; i++){ | |
for(let j=0; j<ROW_AMOUNT; j++){ | |
total += arr[i][j] | |
} | |
} | |
console.log(`Seq Cacl in ${new Date().getTime() - start}, with total ${total}`) | |
} | |
}) | |
worker.postMessage({ | |
rowEnd, | |
rowStart, | |
arr | |
}) | |
} | |
} else { | |
parentPort.once('message', ({ | |
rowStart, | |
rowEnd, | |
arr | |
}) => { | |
let sum = 0; | |
for(let i= rowStart; i < rowEnd; i++){ | |
for(let j=0; j<ROW_AMOUNT; j++) { | |
sum += arr[i][j] | |
} | |
} | |
parentPort.postMessage(sum) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment