arrays - Best practices to take input -
i have written mergesort function, sorted 1 million integers in 750ms, takes 9 seconds take input.
this how have taken input slice, sorted.
code snippet:
array := make([]int,n) := 0; < n; i++ { fmt.scanf("%d",&array[i]) } what need is, efficient way take integers input slice. input contains integers, seperated space or each integer in new-line.
sample input 1:
3 9 1 13 sample input 2:
3 9 1 13
if efficient solution available 1 type of input, suffice
on assumption input space separated signed integers (in base 10), try following:
s := bufio.newscanner(os.stdin) s.split(bufio.scanwords) := 0 s.scan() && < n { dest[i], _ = strconv.parseint(s.text(), 10, 64) i++ } this shows quick benchmark 5 times faster using fmt.scanf. further optimised writing custom split function did not worry parsing utf-8 runes , split on ' '.
Comments
Post a Comment