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
m := make(map[int][128]byte) |
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
const max = 2048 // Defines the threshold | |
func parallelMergesortV2(s []int) { | |
if len(s) <= 1 { | |
return | |
} | |
if len(s) <= max { | |
sequentialMergesort(s) // Calls our initial sequential version | |
} else { // If bigger than the threshold, keeps the parallel version |
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
func parallelMergesortV1(s []int) { | |
if len(s) <= 1 { | |
return | |
} | |
middle := len(s) / 2 | |
var wg sync.WaitGroup | |
wg.Add(2) |
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
func sequentialMergesort(s []int) { | |
if len(s) <= 1 { | |
return | |
} | |
middle := len(s) / 2 | |
sequentialMergesort(s[:middle]) // First half | |
sequentialMergesort(s[middle:]) // Second half | |
merge(s, middle) // Merges the two halves | |
} |
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
runtime.schedule() { | |
// Only 1/61 of the time, check the global runnable queue for a G. | |
// If not found, check the local queue. | |
// If not found, | |
// Try to steal from other Ps. | |
// If not, check the global runnable queue. | |
// If not found, poll network. | |
} |
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
s2 = append(s2, 3) | |
s2 = append(s2, 4) // At this stage, the backing is already full | |
s2 = append(s2, 5) |
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
s1=[0 1 0], s2=[1 0 2] |
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
s2 = append(s2, 2) |
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
s1 := make([]int, 3, 6) // Three-length, six-capacity slice | |
s2 := s1[1:3] // Slicing from indices 1 to 3 |
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
s = append(s, 3, 4, 5) | |
fmt.Println(s) |