lilydjwg / swapview-rosetta

Print swap usage per process. Implemented in various programming languages
493 stars 108 forks source link

Add improvement for Golang #150

Closed Xuanwo closed 5 years ago

Xuanwo commented 5 years ago

strconv.ParseInt is too slow that Go can't beat Rust on my machine now.

lilydjwg commented 5 years ago

Why is strconv.ParseInt slow then? I'm curious.

Xuanwo commented 5 years ago

strconv.ParseInt needs to handle more edge cases like overflow, leading sign and so on. And in order to get all digits, I need to split the string to get them.

May I can do the same thing like

if v < '0' || v > '9' {
    if err != nil {
        continue
        return info, err
    }
    x = append(x, v)
}

And then convert to int64.

lilydjwg commented 5 years ago

But Rust does do all those things too.

Xuanwo commented 5 years ago

What I answered is why strconv.ParseInt slower than v - '0' trick, I have no idea why Go slower than Rust in swapview.

lilydjwg commented 5 years ago

I've just tested and indeed Go's strconv.ParseInt takes twice time as Rust's str::parse::<isize>. I can't tell why because I failed to understand the perf report of the Go program.