英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论