1 year ago
#381590
John Smith
Is taking uint8 from user's input more efficient than taking uint64?
The following code takes a 64-bit unsigned integer from it's user's input:
package main
import (
"fmt"
"bufio"
"os"
"strconv"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Give me a small integer?")
scanner.Scan()
times, _ := strconv.ParseUint(scanner.Text(), 10, 64)
fmt.Printf("%T\n", times) //real 0m1.900s user 0m0.000s sys 0m0.003s
}
and the following takes an 8-bit unsigned integer from it's user:
package main
import (
"fmt"
"bufio"
"os"
"strconv"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Give me a small integer?")
scanner.Scan()
times, _ := strconv.ParseUint(scanner.Text(), 10, 8)
times8bit := uint8(times)
fmt.Printf("%T\n", times8bit) //real 0m2.014s user 0m0.003s sys 0m0.000s
}
Both snippets take approximately the same amount of my CPU time(as shown in the comments by the result of time
).
Is the second snippet using less memory than the first? I would think that creating another variable times8bit
results in the second snippet using even more memory than that used by the first snippet. Is that the case? I am aware that the potential difference in performance is negligent, but is there another, a more efficient way of taking an unsigned 8-bit integer from user's input?
go
user-input
unsigned-integer
0 Answers
Your Answer