快速读取一个大的UTF-8字符串的fmt.Scanf()函数

huangapple go评论85阅读模式
英文:

Fast fmt.Scanf() of a large UTF-8 string

问题

我有一个大约8000000个UTF-8字符的字符串。通过fmt.Scanf()扫描它大约需要10秒,我该如何更快地完成?我有一个用于C语言scanf()函数的Go包装器,由我的老师编写,作为Go的fmt.Scanf()中一些错误的解决方法,它只需要1-2秒,但我不喜欢为这样简单的任务使用附加包。你能否建议一些在纯Go中更快读取字符串的方法?

英文:

I have a string of about 8000000 UTF-8 characters. Scanning it via fmt.Scanf() takes about 10 seconds, how can I do it faster? I have a Go wrapper for C scanf() function that was written by my teacher as a workaround for some bugs in Go's fmt.Scanf(), it works in 1-2 seconds, but I don't like using side packages for such simple tasks. Could you suggest some faster way of reading strings in pure Go?

答案1

得分: 6

找到了解决方案。bufio工作速度更快(因为它是缓冲的,而fmt的函数不是,并且它不解析任何内容):

reader := bufio.NewReader(os.Stdin)
str, _ := reader.ReadString('\n')   // 类似于 fmt.Scanf("%s", &str),但更快
var x, y rune
fmt.Fscanf(reader, "%c %c", &x, &y) // 我需要读取其他内容
                                    // (请参见问题的注释)
                                    // 这很容易,因为我可以使用 fmt.Fscanf

...甚至比C的scanf()包装器更快。

英文:

Found the solution. bufio works much faster (as it's buffered, and fmt's functions are not, and it doesn't parse anything):

reader := bufio.NewReader(os.Stdin)
str, _ := reader.ReadString('\n')   // Like fmt.Scanf("%s", &str), but faster
var x, y rune
fmt.Fscanf(reader, "%c %c", &x, &y) // I need to read something else
                                    // (see comments for the question)
                                    // It's easy, as I can use fmt.Fscanf

...even faster that that C scanf() wrapper.

huangapple
  • 本文由 发表于 2013年2月18日 02:36:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/14924598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定