Created
December 10, 2020 06:35
This my random attempt at an "applied benchmark" in various languages. Say you had to parse a monetary value from a CSV file at a set interval, what would be the quickest way?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/csv" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"strconv" | |
) | |
func main() { | |
file, err := os.Open("../testdata.csv") | |
if err != nil { | |
log.Fatalf("Unable to open csv file: %s", err) | |
} | |
reader := csv.NewReader(file) | |
var totalProfit float64 | |
headersSkipped := false | |
for { | |
row, err := reader.Read() | |
if err == io.EOF { | |
break | |
} | |
if headersSkipped == false { | |
headersSkipped = true | |
continue | |
} | |
profit, _ := strconv.ParseFloat(row[13], 64) | |
totalProfit += profit | |
} | |
fmt.Printf("We made %.2f dollarydoos!", totalProfit) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import parsecsv, strformat, strutils | |
var parser: CsvParser | |
open parser, "testdata.csv" | |
readHeaderRow parser | |
var totalProfit: float64 | |
while readRow parser: | |
totalProfit += parseFloat parser.rowEntry("Total Profit") | |
echo &"We made {totalProfit:.2f} dollarydoos!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() -> Result<(), csv::Error> { | |
let mut reader = csv::Reader::from_path("./testdata.csv")?; | |
let mut total: f64 = 0.0; | |
for result in reader.records() { | |
match result?.get(13) { | |
Some(x) => total += x.parse::<f64>().unwrap(), | |
None => {} | |
} | |
} | |
println!("We made {:.2} dollarydoos!", total); | |
Ok(()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rust (1.48.0): ~800ms | |
; cargo build --release | |
; Cargo.toml: | |
; [profile.release] | |
; lto = "fat" | |
; codegen-units = 1 | |
; panic = "abort" | |
Nim (1.4.2): ~800ms | |
; nim c --d:danger --d:lto --opt:speed --gc:none csvparser.nim | |
Go (1.15.3): ~850ms | |
; go build csvparser.go |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment