golangの書き込みファイルテスト
1797 ワード
package main
import (
"os"
"strings"
"time"
"fmt"
"strconv"
)
//
func timeCost() func() {
start := time.Now()
return func() {
tc := time.Since(start)
fmt.Printf("time cost = %v
", tc)
}
}
func sum(n int, fd *os.File) {
defer timeCost()() // , timeCost() ,
for i:=1; i <= n; i++ {
stri := strconv.Itoa(i)
content := " " + stri
fd_time := time.Now().Format("2006-01-02 15:04:05")
fd_content := strings.Join([]string{"======",fd_time,"=====",content,"
"},"")
buf := []byte(fd_content)
fd.Write(buf)
}
}
func main() {
fd , _ := os.OpenFile("xyz2.txt",os.O_RDWR|os.O_CREATE|os.O_APPEND,0644)
sum(10000,fd)
fd.Close()
}
1 w本の記録を圧測し、appendはファイルを書くのに時間を費やす.