大UTF-8字符串的快速fmt.Scanf() [英] Fast fmt.Scanf() of a large UTF-8 string

查看:76
本文介绍了大UTF-8字符串的快速fmt.Scanf()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大约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?

推荐答案

找到了解决方案. bufio的工作速度更快(因为它被缓冲了,而fmt的功能却没有,并且它不解析任何内容):

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

...甚至比C scanf()包装器还要快.

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

这篇关于大UTF-8字符串的快速fmt.Scanf()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆