go 메모리 정렬

2724 단어

설명:


Source And Sink

package pipeline

import (
    "encoding/binary"
    "fmt"
    "io"
    "math/rand"
    "sort"
    "time"
)

var startTime time.Time

func Init() {
    startTime = time.Now()
}

func ArraySource(a ...int) chan int {
    out := make(chan int)
    go func() {
        for _, v := range a {
            out  0 {
                v := int(binary.BigEndian.Uint64(buffer))
                out = chunkSize) {
                break
            }
        }
        close(out)
    }()
    return out
}

func WriteSink(writer io.Writer, in 

testfile 파일 생성


테스트 파일 생성(small.in/large.in), 대응하는 작은 파일과 큰 파일
package main

import (
    "bufio"
    "fmt"
    "os"
    "pipeline"
)

// 
//const FILE_NAME = "small.in"
//const N = 64

// 
const FILE_NAME = "large.in"
const N = 1000000

func main() {
    file, e := os.Create(FILE_NAME)
    if e != nil {
        panic(e)
    }
    defer file.Close()

    p := pipeline.RandomSource(N)
    writer := bufio.NewWriter(file)
    pipeline.WriteSink(writer, p)
    writer.Flush()

    file, e = os.Open(FILE_NAME)
    if e != nil {
        panic(e)
    }
    defer file.Close()

    p = pipeline.ReaderSource(file, -1)

    count := 0
    for v := range p {
        count++
        if count < 100 {
            fmt.Println(v)
        }
    }
}

// 
func main0() {
    p := pipeline.Merge(
        pipeline.InMemSort(pipeline.ArraySource(8, 2, 3, 0, 1)),
        pipeline.InMemSort(pipeline.ArraySource(9, 7)),
    )
    for v := range p {
        fmt.Println(v)
    }
}

네트워크 정렬 사용

package pipeline

import (
    "bufio"
    "net"
)

func NetworkSink(addr string, in 

테스트 주 방법

package main

import (
    "bufio"
    "fmt"
    "os"
    "pipeline"
    "strconv"
)

func main2() {
    p := createNetworkPipeline("large.in", 8000000, 4)

    writeToFile(p, "large-networkpipeline.out")
    printFile("large-networkpipeline.out")
}

func main1() {
    p := createPipeline("large.in", 8000000 , 4)
    writeToFile(p, "large-pipeline.out")
    printFile("large-pipeline.out")
}

func printFile(filename string) {
    file, err := os.Open(filename)
    if err != nil {
        panic(err)
    }
    defer file.Close()

    p := pipeline.ReaderSource(file, -1)
    count := 0
    for v := range p {
        if count < 100 {
            fmt.Println(v)
            count ++
        }
    }
}

func writeToFile(p 

좋은 웹페이지 즐겨찾기