- 论坛徽章:
- 0
|
本帖最后由 aswjh 于 2015-03-16 13:16 编辑
用go语言做了一个,不太熟,看看性能如何
用gccgo -g -o test test.go -O3比go的默认编译器要快30%
- package main
- import (
- //"runtime"
- "fmt"
- "os"
- "bufio"
- "io"
- "strings"
- "strconv"
- )
- func run(fn string) {
- fp, err := os.Open(fn)
- defer fp.Close()
- MAXCOL := 500
- max := [500]int{}
- min := [500]int{}
- sum := [500]int{}
- empty := [500]int{}
- nr, colcnt := 0, 0
- if err == nil {
- buf := bufio.NewReader(fp)
- bufio.NewReaderSize(buf, 6553600)
- for {
- data, _, err := buf.ReadLine()
- if err != nil || err == io.EOF {
- break
- }
- nr++
- if nr%1000000==0 {
- fmt.Println("处理到:", nr, "行")
- }
- cols := strings.Split(string(data), ",")
- leng := len(cols)
- for c := 3; c < leng; c++ {
- num, err := strconv.Atoi(cols[c])
- if c > colcnt {
- if err == nil {
- max[c], min[c], sum[c] = num, num, num
- } else {
- empty[c] = 1
- }
- colcnt = c
- } else {
- if err == nil {
- if num > max[c] {
- max[c] = num
- } else if num < min[c] {
- min[c] = num
- }
- sum[c] += num
- } else {
- min[c] = 0
- empty[c]++
- }
- }
- }
- for c := leng; c< MAXCOL; c++ {
- min[c] = 0
- empty[c]++
- }
- }
- fmt.Println("处理结束.共", nr, "行")
- for i, x := range sum {
- if x > 0 || empty[i] < nr {
- fmt.Println(fmt.Sprintf("%d,%d,%d,%d,%d", i+1, max[i], min[i], x, empty[i]))
- }
- }
- } else {
- fmt.Print("打开文件出错:", fn)
- }
- }
- func main() {
- //runtime.GOMAXPROCS(5)
- for _, f := range os.Args[1:] {
- fmt.Println("正在处理:", f)
- run(f)
- }
- }
复制代码 |
|