代码中的进程缓慢

时间:2017-11-30 16:37:46

标签: awk

你可以帮助改善这段代码更快..在我的文件中使用50000行需要花费很多时间。

感谢您的帮助

输入

let url = "YOUR_URL"

let yourData = "WHATEVER CUSTOM VAL YOU WANT TO PASS"

var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "PUT"
//request.setValue("application/json", forHTTPHeaderField: "Content-Type") //This is if you want to have headers. But it doesn't look like you need them. 

request.httpBody  = yourData
/*
do {
    request.httpBody  = try JSONSerialization.data(withJSONObject: yourData)
} catch {
    print("JSON serialization failed:  \(error)")
}
*/
Alamofire.request(request).responseJSON {(response) in
    if response.response?.statusCode == 200 {
        print ("pass")
    }else{
        print ("fail")
    }
    completionHandler((response.response?.statusCode)!)

    /*
    switch response.result {
    case .success(let value):
        print ("success: \(value)")
    case .failure(let error):
        print("error: \(error)")
    }
    */
}

输出

17/11/27 03:13:50:480000
17/11/27 03:12:54:380000
17/11/27 03:14:39:980000

我的代码

1195787648480000
1195787592380000
1195787697980000

请帮助我改进代码以更快地获得结果。

非常感谢。

2 个答案:

答案 0 :(得分:3)

使用单个GNU awk 进程:

awk -F'[[:space:]]*|/|:' -v ts=$(date -d'01/06/1980 00:00:00' +%s) -v lap=18 '{ 
     print (mktime(sprintf("20%d %d %d %d %d %d",$1,$2,$3,$4,$5,$6)) - ts)+lap $NF 
}' file

输出:

1195791248480000
1195791192380000
1195791297980000

享受)

答案 1 :(得分:2)

gawk

类似
$ awk -F'[/: ]' -v ts=$(date -d'01/06/1980' +%s)  \
                -v lap=18 '{ms=$NF; $NF=""; d=sprintf(20$0); 
                            print mktime(d)+lap-ts ms}' file

1195787648480000
1195787592380000
1195787697980000