Hi,

I made the following Go program (main.go). But it is still slower than awk 
or cut. I know that `cut` uses string manipulation tricks to make it run 
very efficiently.

https://github.com/coreutils/coreutils/blob/master/src/cut.c

But I am not sure how to do the same thing in golang. Could anybody let me 
know what is wrong with my go program and how to make it run faster?

$  ./main_.sh 
time ./main.go 10 < "$infile" > /dev/null

real    0m1.418s
user    0m0.976s
sys    0m0.433s
time cut -f 10 < "$infile" > /dev/null

real    0m0.249s
user    0m0.219s
sys    0m0.026s
time awk -v FS='\t' -v OFS='\t' -e '{ print $10 }' < "$infile" > /dev/null

real    0m1.156s
user    0m1.123s
sys    0m0.031s

==> main.go <==
#!/usr/bin/env gorun
// vim: set noexpandtab tabstop=2:
package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"
    "strconv"
)

func main() {
    idx, _ := strconv.Atoi(os.Args[1])
    col := idx - 1

    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        line := strings.TrimRight(scanner.Text(), "\n")
        fields := strings.Split(line, "\t")
        fmt.Printf("%s\n", fields[col])
    }
}

==> main_.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
infile=$(mktemp)
seq 10000000 | paste -s -d $'\t\t\t\t\t\t\t\t\t\n' > "$infile"
time ./main.go 10 < "$infile" > /dev/null
time cut -f 10 < "$infile" > /dev/null
time awk -v FS='\t' -v OFS='\t' -e '{ print $10 }' < "$infile" > /dev/null


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to