-
-
Save e-dard/0667a5edc259e02c4d25 to your computer and use it in GitHub Desktop.
Ruby csv writing time ~34 seconds. Go csv writing time ~ 1.8 seconds.
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 ( | |
"fmt" | |
"time" | |
"strconv" | |
"os" | |
"math/rand" | |
"encoding/csv" | |
) | |
const chars = "ABCDEFGHIJKLMNOP, " | |
func gen_data(num_rows, num_cols int) [][]string{ | |
data := make([][]string, num_rows) | |
for i := range data { | |
data[i] = make([]string, num_cols) | |
for j := range data[i] { | |
if j < (num_cols/2.0) { | |
str := make([]uint8, 10) | |
for s := range str { | |
str[s] = chars[rand.Intn(len(chars))] | |
} | |
data[i][j] = string(str[0:10]) | |
} else { | |
data[i][j] = strconv.FormatFloat(rand.Float64() * 100.0, 'f', 10, 32) | |
} | |
} | |
} | |
return data | |
} | |
func write_csv(data [][]string) { | |
f, err := os.Create("/tmp/go-out.csv") | |
if err != nil { | |
return | |
} | |
defer f.Close() | |
out := csv.NewWriter(f) | |
out.WriteAll(data) | |
} | |
func main() { | |
data := gen_data(100000, 40) | |
start := time.Now() | |
write_csv(data) | |
total := time.Since(start) | |
fmt.Println(total) | |
} |
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
require 'csv' | |
dt = [:str] * 20 | |
dt += [:float] * 20 | |
def time | |
start = Time.now | |
yield | |
puts "Took #{Time.now - start}" | |
end | |
def gen_rows(data_types, rows) | |
charset = "ABCDEFGHIJKLMNOP, ".split("") | |
result = [] | |
rows.times do |i| | |
row = [] | |
data_types.each do |dt| | |
case dt | |
when :str | |
row << (0...10).map{charset.sample}.join | |
when :float | |
row << (rand * 100).round(10) | |
end | |
end | |
result << row | |
end | |
result | |
end | |
time do | |
DATA = gen_rows(dt, 100000) | |
end | |
CSV.open("/tmp/foo.csv", "wb") do |csv| | |
time do | |
DATA.each do |row| | |
csv << row | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment