Faster input scanning

前端 未结 3 1366
清酒与你
清酒与你 2020-12-06 17:28

I am attempting to solve the SPOJ question that can be found here

Following is my solution:

package main

import \"fmt\"
import \"bufio\"
import \"os         


        
3条回答
  •  难免孤独
    2020-12-06 18:02

    You can use bufio.Scanner to read lines from the input.

    And since we're always reading numbers, we can create a highly optimized converter to get the number. We should avoid using Scanner.Text() which creates a string as we can obtain the number just from the raw bytes returned by Scanner.Bytes(). Scanner.Text() returns the same token as Scanner.Bytes() but it first converts to string which is obviously slower and generates "garbage" and work for the gc.

    So here is a converter function which obtains an int from the raw bytes:

    func toInt(buf []byte) (n int) {
        for _, v := range buf {
            n = n*10 + int(v-'0')
        }
        return
    }
    

    This toInt() works because the []byte contains the UTF-8 encoded byte sequence of the string representation of the decimal format of the number, which contains only digits in the range of '0'..'9' whose UTF-8 encoded bytes are mapped one-to-one (one byte is used for one digit). The mapping from digit to byte is simply a shift: '0' -> 48, '1' -> 49 etc.

    Using this your complete application:

    package main
    
    import (
        "bufio"
        "fmt"
        "os"
    )
    
    func main() {
        var n, k, c int
        scanner := bufio.NewScanner(os.Stdin)
    
        scanner.Scan()
        fmt.Sscanf(scanner.Text(), "%d %d", &n, &k)
    
        for ;n > 0; n-- {
            scanner.Scan()
            if toInt(scanner.Bytes())%k == 0 {
                c++
            }
        }
    
        fmt.Println(c)
    }
    
    func toInt(buf []byte) (n int) {
        for _, v := range buf {
            n = n*10 + int(v-'0')
        }
        return
    }
    

    This solution is about 4 times faster than calling strconv.Atoi() for example.

    Notes:

    In the above solution I assumed input is valid, that is it always contains valid numbers and contains at least n lines after the first (which gives us n and k).

    If the input is closed after n+1 lines, we can use a simplified for (and we don't even need to decrement and rely on n):

    for scanner.Scan() {
        if toInt(scanner.Bytes())%k == 0 {
            c++
        }
    }
    

提交回复
热议问题