UTF-8 Big Line Fast Scan

I have a string of about 8,000,000 UTF-8 characters. Scanning through fmt.Scanf() takes about 10 seconds, how can I do it faster? I have a Go shell for the C scanf() function, which was written by my teacher as a workaround for some errors in Go fmt.Scanf (), it works after 1-2 seconds, but I don’t like using side packages for such simple tasks. Could you suggest a faster way to read lines in pure Go?

+1
source share
1 answer

Found a solution. bufio works much faster (since it is buffered, and fmt functions are missing, and it does not 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 easy, as I can use fmt.Fscanf 

... even faster than the C scanf() wrapper.

+6
source

Source: https://habr.com/ru/post/1487239/


All Articles