go 用 strings.Builder
代替普通的 +
号拼接, 在本人的项目中性能有 8 ~15倍的提升,之前写的太差了。
golang code
代码是很简单的
var puts fmt.Sprintf
var builder strings.Builder
builder.WriteString(puts("0x%x,", blkHash))
builder.WriteString(puts("%d,", blkHeader.Number))
builder.WriteString(puts("0x%x,", getFromAddr(tx, big.NewInt(int64(i)))))
builder.WriteString(puts("0x%x,", txGas))
builder.WriteString(puts("0x%x,", txGasPrice))
builder.WriteString(puts("0x%x,", tx.Hash()))
builder.WriteString(puts("0x%x,", txInput))
builder.WriteString(puts("%v,", txNonce))
builder.WriteString(puts("%v,", txTo))
builder.WriteString(puts("%v,", txValue))
builder.WriteString(puts("%d\r\n", blkHeader.Time))
builder.String()
参考:
https://juejin.im/entry/5ad0696751882555784e60d3
有疑问加站长微信联系(非本文作者)