Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shell and Swift versions of a little script to compare Swift benchmark output files
/*
Shell version:
paste -d , oldoutput.txt output.txt | awk -F "," '{printf "%s %s %s %f\n", $2, $8, $16, $8 / $16}' | sort -k4 | egrep -v "(1\.0|0\.9[1-9])"
*/
import Foundation
// "paste -d , oldfile.txt newfile.txt"
let paths = CommandLine.arguments[1...2].map(URL.init(fileURLWithPath:))
let files = paths.map {
try! String(contentsOf: $0.standardized, encoding: .utf8)
}
let lines = files.map { $0.split(separator: "\n") }
let merged = zip(lines[0], lines[1]).map { "\($0),\($1)" }
// awk -F "," '{printf "%s %s %s %f\n", $2, $8, $16, $8 / $16}'
// Almost, anyway… leaving the last field as a Double for now
let results = merged.map { (line) -> (text: String, ratio: Double) in
let columns = line.split(separator: ",")
return (
"\(columns[1]) \(columns[7]) \(columns[15])",
Double(columns[7])! / Double(columns[15])!
)
}
//sort -k4
let sorted = results.sorted { $0.ratio < $1.ratio }
// Last bit of the 'awk' invocation since we're done with using them numerically
let formatted = sorted.map { "\($0.text) \($0.ratio)" }
//egrep -v "(1\.0|0\.9[1-9])"
let filtered = formatted.filter {
$0.range(of: #"(1\.0|0\.9[1-9])"#, options: .regularExpression) == nil
}
print(filtered)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment