@
lsk569937453 这个数据量就直接往内存怼就可以了,高深的我不会...
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"time"
)
func ubtoa(dst []byte, start int, v byte) int {
if v < 10 {
dst[start] = v + '0'
return 1
} else if v < 100 {
dst[start+1] = v%10 + '0'
dst[start] = v/10 + '0'
return 2
}
dst[start+2] = v%10 + '0'
dst[start+1] = (v/10)%10 + '0'
dst[start] = v/100 + '0'
return 3
}
func main() {
rand.Seed(time.Now().Unix())
t := time.Now()
arr := make([]int, 100000000)
for i := range arr {
arr[i] = i
}
fmt.Println("init array", time.Since(t))
rand.Shuffle(len(arr), func(i, j int) { arr[i], arr[j] = arr[j], arr[i] })
fmt.Println("shuffle array", time.Since(t))
b := make([]byte, 16*100000000)
pos := 0
for i := range arr {
pos += ubtoa(b, pos, byte(arr[i]>>24))
b[pos] = '.'
pos++
pos += ubtoa(b, pos, byte(arr[i]>>16))
b[pos] = '.'
pos++
pos += ubtoa(b, pos, byte(arr[i]>>8))
b[pos] = '.'
pos++
pos += ubtoa(b, pos, byte(arr[i]))
b[pos] = '\n'
pos++
}
fmt.Println("write to buffer", time.Since(t))
ioutil.WriteFile("ip.txt", b[:pos], 0600)
fmt.Println("write to file", time.Since(t))
}