用Golang与perl脚本比较, 初想至多差一倍吧...结果可不是一般的坑爹, 简直就是坑爷了.

Perl脚本

#!/bin/bash

source /etc/profile;

function extractAndZip(){
        _debug "$FUNCNAME,$@";
        local logFile="${2}"
        local gzipFile="${1}"
        perl -ne 'if(m/([^ ]*) \- ([^ ]*) \[([^ ]*) [\+\-][0-9]{4}\] \"(\-|(([^ ]*) )?([^\?\;\% ]*)([\?\;\%]([^ ]*))?( ([^\"]*))?)\" ([^ ]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([^ ]*) (\-|([^\-]+))/){printf("%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\001%s\n", ${1}, ${2}, ${3}, ${6}, ${7}, ${9}, ${11}, ${12}, ${13}, ${14}, ${15}, ${16}, ${17}*1000, ${19}*1000)}' ${logFile} | gzip > ${gzipFile};
}

extractAndZip "$@"

www-data@dc26:/data2/rsynclog/gotest$ time bash perl.sh result.gz 2014-06-17+yyexplorer+58.215.138.18+yyexplorer-access.log
/data/sa/profile_common: line 23: ulimit: open files: cannot modify limit: Operation not permitted
perl.sh: line 6: _debug: command not found

real    4m5.222s
user    5m54.630s
sys     0m9.720s

6分钟全部搞定...

golang代码:

package main

import (
    "bufio"
    "compress/gzip"
    "fmt"
    "os"
    "regexp"
    "strconv"
    //"strings"
)

var recordRegExp = regexp.MustCompile(`([^ ]*) \- ([^ ]*) \[([^ ]*) [\+\-][0-9]{4}\] \"(\-|(([^ ]*) )?([^\?\;\% ]*)([\?\;\%]([^ ]*))?( ([^\"]*))?)\" ([^ ]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([^ ]*) (\-|([^\-]+))`)

func toInt(str string) int {
    val, err := strconv.Atoi(str)
    if err != nil {
        return val
    }
    return 0
}

func main() {
    if len(os.Args) < 3 {
        fmt.Println("Usage:", os.Args[0], "<out_zip_file>", "<in_txt_file1...>")
        os.Exit(1)
    }

outZipFile, err := os.Create(os.Args[1])
    if err != nil {
        fmt.Errorf("错误:%s\n", err.Error())
        os.Exit(1)
    }
    defer outZipFile.Close()

inTxtFiles := make([]*os.File, len(os.Args)-2)
    for _, path := range os.Args[2:] {
        file, err := os.Open(path)
        if err != nil {
            fmt.Errorf("错误:%s\n", err.Error())
            os.Exit(1)
        }
        defer file.Close()
        inTxtFiles = append(inTxtFiles, file)
    }

zipIo := gzip.NewWriter(outZipFile)
    defer zipIo.Close()
    out := bufio.NewWriter(zipIo)
    for _, file := range inTxtFiles {
        scan := bufio.NewScanner(file)
        for scan.Scan() {
            line := scan.Bytes()
            items := recordRegExp.FindSubmatch(line)
            out.Write(items[1])
            out.Write([]byte("\t"))
            out.Write(items[2])
            out.Write([]byte("\t"))
            out.Write(items[3])
            out.Write([]byte("\t"))
            out.Write(items[6])
            out.Write([]byte("\t"))
            out.Write(items[7])
            out.Write([]byte("\t"))
            out.Write(items[9])
            out.Write([]byte("\t"))
            out.Write(items[11])
            out.Write([]byte("\t"))
            out.Write(items[12])
            out.Write([]byte("\t"))
            out.Write(items[13])
            out.Write([]byte("\t"))
            out.Write(items[14])
            out.Write([]byte("\t"))
            out.Write(items[15])
            out.Write([]byte("\t"))
            out.Write(items[16])
            out.Write([]byte("\t"))
            out.Write([]byte(strconv.Itoa(toInt(string(items[17])) * 1000)))
            out.Write([]byte("\t"))
            out.Write([]byte(strconv.Itoa(toInt(string(items[19])) * 1000)))
            out.Write([]byte("\n"))
        }
        out.Flush()
    }

}
结果手工kill时:

16m才完成了3分之1左右...坑你爷了...

golang初试:坑爷的的更多相关文章

  1. 初生牛犊不怕虎 golang入坑系列

    读前必读,下面所有内容都是来自这里. 放到这里的目的,就是为了比对一下,哪里的读者多.平心而论,同样的Markdown,博客园排版真心X看,怎么瞅怎么X看.(X := '难' || X :='耐' | ...

  2. golang的哪些坑爷事: package实践

    在golang中package是个困惑的概念, 特别是package还可以与folder不同名, 委实让我恶心了一把. 关于golang的package的最佳实践: package is folder ...

  3. 入坑第二式 golang入坑系统

    史前必读: 这是入坑系列的第二式,如果错过了第一式,可以去gitbook( https://andy-zhangtao.gitbooks.io/golang/content/ )点个回放,看个重播.因 ...

  4. 维多利亚的秘密 golang入坑系统

    原文在gitbook,字字原创,版权没有,转载随意. 在写本文的前一天,2017维密在上海开始了. 为了纪念屌丝界的盛世,特为本节起名维多利亚的秘密.现在的社会,要想出名只有抓眼球.所以写份技术文章, ...

  5. 崩溃 golang入坑系列

    早上(11.30)收到邮件,Vultr东京机房网络故障.当时搭建SS时,考虑到了机房故障.所以特意分出了日本和香港两条线路.但千算万算,忘记数据库还在东京机房中. 现在网络故障,SS服务器无法读取数据 ...

  6. jupyter notebook安装/代码补全/支持golang 踩坑记

    安装(不要用root) 安装anaconda3,然后ln -s bin目录下的jupyter命令到/usr/bin目录下 生成密码备用 敲ipython进入交互终端 In [1]: from note ...

  7. 维多利亚的秘密 golang入坑系列

    原文在gitbook,字字原创,版权没有,转载随意. 在写本文的前一天,2017维密在上海开始了. 为了纪念屌丝界的盛世,特为本节起名维多利亚的秘密.现在的社会,要想出名只有抓眼球.所以写份技术文章, ...

  8. 入坑第二式 golang入坑系列

    史前必读: 这是入坑系列的第二式,如果错过了第一式,可以去gitbook( https://andy-zhangtao.gitbooks.io/golang/content/ )点个回放,看个重播.因 ...

  9. golang深坑记录

    go深坑:1.gin.context.JSON,如果没有make数组时,数组返回为null,make后,数组为[]2.json.Number转int64类型 datatemp.(json.Number ...

随机推荐

  1. css显示出三角形

    其实非常简单,就是设置一个div 让div的宽度和高度都设置为0, 然后为div设置一个border 因为角部位,比如我设置border-left和border-top 角部分是各自占用一半, 所以当 ...

  2. JS 点击按钮后弹出遮罩层,有关闭按钮

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  3. js跳转页面方法(转)

    <span id="tiao">3</span><a href="javascript:countDown"></a& ...

  4. Hadoop SecondaryNameNode备份及恢复

    1.同步各个服务器时间 yum install ntp ntpdate ntp.fudan.edu.cn hdfs-site.xml配置 如果没有配置这一项,hadoop默认是0.0.0.0:5009 ...

  5. c# 数据库操作学习

    一. 如何处理数据库连接 1. 数据库连接可以分为“物理连接”和“逻辑连接”(默认使用连接池的情况下Pooling=true): 物理连接:创建数据库连接时,默认会有一定数量的物理连接(默认Min P ...

  6. Winform登录、控制软件只运行一次、回车登录

    Winform登录对很多程序猿来说都有些困惑,登录进入主窗体后要销毁登录窗体,而不是隐藏哦,怎么实现呢? 先贴一段Program.cs的代码 static void Main() { Mutex mu ...

  7. [leetcode]_Search Insert Position

    题目:查找元素target插入一个数组中的位置. 代码: public int searchInsert(int[] A, int target) { int len = A.length; int ...

  8. js日期格式化方法 dateFormatFn

    var dateFormatFn=function(val,fmt){ var _this = new Date(val); console.log(_this,_this.getFullYear() ...

  9. HUE 忘记密码

    解决方法: 启动HUE的Shell /opt/cloudera/parcels/CDH/lib/hue/build/env/bin/hue shell from django.contrib.auth ...

  10. Incorrect column name 'productid '

    #1166 - Incorrect column name 'productid ' 解决方法:字段是复制的吧,复制的里面应该是有空格吧?检查一下,去掉就可以了哟,呵呵.