快速扫描大型UTF-8字符串 [英] Fast scanning of a large UTF-8 string

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

问题描述

我有一个大约8000000个UTF-8字符的字符串。通过 fmt.Scanf()扫描大约需要10秒钟,我该如何更快地做到这一点?我有一个由我的老师编写的用于C scanf()函数的Go包装,作为Go的fmt.Scanf()中的一些错误的解决方法,它适用于1-2秒,但我不喜欢使用侧包进行这样简单的任务。你能提出一些更快的方式来阅读纯Go中的字符串吗? 解决方案

找到解决方案。 bufio 的工作速度要快得多(因为它被缓冲了, fmt 的函数不是,它不会解析任何东西):

 读者:= bufio.NewReader(os.Stdin)
STR,_:= reader.ReadString( '\\\
')//赞fmt.Scanf( %S,&安培; STR),但速度更快
变种的x,y符
fmt.Fscanf(读取器, %C%C ,& x,& y)//我需要读取其他的
//(请参阅问题的注释)
//很简单,因为我可以使用fmt.Fscanf

...甚至更快,即C scanf() wrapper 。


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?

解决方案

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.

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

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