Skip to content

Instantly share code, notes, and snippets.

@borakasmer
Created August 18, 2020 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borakasmer/adaeaa15b1da00a303d5a8ed8c3ec321 to your computer and use it in GitHub Desktop.
Save borakasmer/adaeaa15b1da00a303d5a8ed8c3ec321 to your computer and use it in GitHub Desktop.
package concurrency
import (
"container/list"
"fmt"
"strings"
"sync"
"time"
)
func makeRangeList(min,max int) *list.List{
queue := list.New()
for i:= 0;i<max;i++ {
queue.PushBack(min+i)
}
return queue
}
func startConcurrency(max int,duration int) int64{
start := time.Now()
numberList := makeRangeList(1,max)
var wg sync.WaitGroup
wg.Add(numberList.Len())
for numberList.Len()>0{
//wg.Add(1)
e := numberList.Front()
go printNumber(e,&wg,duration)
numberList.Remove(e)
}
wg.Wait()
finish :=time.Now()
diff := finish.Sub(start)
fmt.Println(fmt.Sprintf("Örnek {%d,%d},Concurrent Geçen Toplam Süre:%d",max,duration,diff.Milliseconds()))
fmt.Println(strings.Repeat("-",100))
return diff.Milliseconds()
}
func startSingle(max int,duration int) int64{
start := time.Now()
numberList := makeRangeList(1,max)
for numberList.Len()>0{
e := numberList.Front()
d := time.Duration(duration) * time.Second
time.Sleep(d)
//fmt.Println(e.Value)
numberList.Remove(e)
}
finish :=time.Now()
diff := finish.Sub(start)
fmt.Println(fmt.Sprintf("Örnek {%d,%d},Single Geçen Toplam Süre:%d",max,duration,diff.Milliseconds()))
fmt.Println(strings.Repeat("-",100))
return diff.Milliseconds()
}
func printNumber (number *list.Element,wg *sync.WaitGroup,duration int){
d := time.Duration(duration) * time.Second
time.Sleep(d)
//fmt.Println(number.Value)
wg.Done()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment