Skip to content

Instantly share code, notes, and snippets.

@windhc
Last active January 28, 2021 14:05
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 windhc/e4506e9f66de36583ed57291491faf0b to your computer and use it in GitHub Desktop.
Save windhc/e4506e9f66de36583ed57291491faf0b to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"time"
)
var allS1OutWriterMap = make(map[string]*bufio.Writer)
var allS1OutFileMap = make(map[string]*os.File)
func main() {
now := time.Now()
line := "abcdeabcdeabcdeabcde\n"
for i := 0; i < 4200; i++ {
for j := 0; j < 2500; j++ {
filePath := "D:\\test\\s1out\\" + strconv.Itoa(i) + ".txt"
writer := allS1OutWriterMap[filePath]
if writer == nil {
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return
}
allS1OutFileMap[filePath] = f
writer = bufio.NewWriter(f)
allS1OutWriterMap[filePath] = writer
}
writer.WriteString(line)
}
}
fmt.Println("\nWrite over - ", time.Since(now))
// Flush
for key := range allS1OutWriterMap {
err := allS1OutWriterMap[key].Flush()
if err != nil {
fmt.Println(err)
}
}
fmt.Println("\nFlush over - ", time.Since(now))
// Close
for key := range allS1OutFileMap {
err := allS1OutFileMap[key].Close()
if err != nil {
fmt.Println(err)
}
}
fmt.Println("\nClose over - ", time.Since(now))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment