28
loading...
This website collects cookies to deliver better user experience
type Coin struct {
ID string `json:"id" gorethink:"id,omitempty"`
Name string `json:"name" gorethink:"name" mapstructure:"CoinName"`
Symbol string `json:"symbol" gorethink:"symbol"`
Algorithm string `json:"algorithm" gorethink:"algorithm"`
Price float64 `json:"price" gorethink:"price"`
}
type Data struct {
Data map[string]Coin
}
time
. This would be done like so:time go run . -fetch
fetch
-flag were to tell it to populate my database, stupid name now that I think about it, but it's not the one to be questioned as for now.2.5m
, that's a lot of waiting time a busy little bee like me. The total response is below:go run . -fetch 2.24s user 1.12s system 2% cpu 2:25.31 total
func FetchCrypto(d *db.DB) {
dat, _ := ioutil.ReadFile("coins.json")
var data Data
json.Unmarshal(dat, &data)
for _, c := range data.Data {
c.ID = ""
if c.Algorithm == "N/A" {
c.Algorithm = ""
}
r.Table("coin").
Insert(c).
Exec(d.S)
}
}
go run . -fetch 1.89s user 0.81s system 47% cpu 5.683 total
func FetchCryptoV2(d *db.DB) {
dat, _ := ioutil.ReadFile("coins.json")
var data Data
json.Unmarshal(dat, &data)
var wg sync.WaitGroup
batch := 100
coins := []coin.Coin{}
for _, c := range data.Data {
coins = append(coins, c)
}
length := len(coins)
for i := 0; i < length; i += batch {
wg.Add(1)
go func(i int) {
b := coins[i:]
if len(b) > batch {
b = b[:batch]
}
for _, c := range b {
c.ID = ""
if c.Algorithm == "N/A" {
c.Algorithm = ""
}
r.Table("coin").
Insert(c).
Exec(d.S)
}
wg.Done()
}(i)
}
wg.Wait()
}
go run . -fetch 1.02s user 0.26s system 28% cpu 4.488 total
func FetchCryptoV3(d *db.DB) {
dat, _ := ioutil.ReadFile("coins.json")
var data Data
json.Unmarshal(dat, &data)
coins := []coin.Coin{}
for _, c := range data.Data {
c.ID = ""
if c.Algorithm == "N/A" {
c.Algorithm = ""
}
coins = append(coins, c)
}
r.Table("coin").
Insert(coins).
Exec(d.S)
}
10
, this more than halved the time it took to insert into the database, thus making it faster than the standard one now.2.9s
.go run . -fetch 1.67s user 0.49s system 74% cpu 2.917 total