You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.3 KiB
57 lines
1.3 KiB
6 years ago
|
/*
|
||
|
Landesamt für Geoinformation und Landentwicklung, Referat 35
|
||
|
Einführung in die Sprache Go, 25.7.2017
|
||
|
Lektion 4: Nebenläufigkeit
|
||
|
*/
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math"
|
||
|
"math/rand"
|
||
|
"runtime"
|
||
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
anzahlTöpfchen int8 = 25
|
||
|
anzahlKugeln int32 = 100000
|
||
|
)
|
||
|
|
||
|
var timestamp [2]int64
|
||
|
|
||
|
var wg sync.WaitGroup // !
|
||
|
|
||
|
func main() {
|
||
|
runtime.GOMAXPROCS(runtime.NumCPU()) // !
|
||
|
timestamp[0] = time.Now().UnixNano()
|
||
|
var töpfchenAlle []int = make([]int, anzahlTöpfchen, anzahlTöpfchen)
|
||
|
for k := (int32)(1); k <= anzahlKugeln; k++ {
|
||
|
wg.Add(1)
|
||
|
go zeigeMirDenGauß(int(anzahlKugeln), int(anzahlTöpfchen), &töpfchenAlle) // !
|
||
|
}
|
||
|
wg.Wait() // !
|
||
|
fmt.Println(töpfchenAlle)
|
||
|
timestamp[1] = time.Now().UnixNano()
|
||
|
fmt.Printf("\nProzess ist fertig und hat %v Sekunden gedauert!\n\n", float64(timestamp[1]-timestamp[0])*math.Pow(10, -9))
|
||
|
}
|
||
|
|
||
|
func zeigeMirDenGauß(anzahlKugeln int, anzahlTöpfchen int, töpfchenAlle *[]int) { // kleines z genügt
|
||
|
defer wg.Done() // !
|
||
|
var töpfchenTmpZähler int
|
||
|
|
||
|
for i := 0; i < anzahlTöpfchen-1; i++ {
|
||
|
if yesnotrigger := func() bool {
|
||
|
if rand.New(rand.NewSource(time.Now().UnixNano())).Float64() < 0.5 {
|
||
|
return true
|
||
|
} else {
|
||
|
return false
|
||
|
}
|
||
|
}(); yesnotrigger == true {
|
||
|
töpfchenTmpZähler++
|
||
|
}
|
||
|
}
|
||
|
(*töpfchenAlle)[töpfchenTmpZähler]++
|
||
|
}
|