On Fri, Mar 14, 2014 at 07:42:53PM -0400, Andrew Feren wrote:
> I've never seen GO before this thread, but after a glance at the GO web
> site, I suspect the difference may have more to do with concurrency than
> compiling. Perl is written in C and natively pretty good at file and string
> manipulation. My gut says a 2x win for this problem just by using GO compiled
> to a "static binary" is much too big a difference.
No, that's not it. The difference really is due to the compilation.
Any interpreter will have overhead that a compiled program will not.
In perl's case, there's even more overhead for providing dynamic memory
management, relaxed typing, etc. 2x is actually not bad, all things
considered. I bet a C version would be even faster; but at the cost of
managing memory and implementing your own hash, string and array data
structures. However, you can tune all of that to the problem set so it
really is a trade-off.
The piper must be paid, you just get to choose when and how.
> Is the code for the GO program being compared to the posted perl script also
> available?
It is now. As you can see, it's a straightforward translation of the original
into idiomatic Go.
Even without comments it should be readable to anyone who has programmed any
Algol derived language.
I've been using Go for about 2 years now and I really like it.
It fits Alan Kay's maxim: simple things are simple and complex things are
possible.
To which I would add also that impossible things are imaginable.
More prosaic, but equally important, deployment is just a file copy.
Dependency management is pushed to development time where, I think, it rightly
belongs.
-Gyepi
__DATA__
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
type Counter map[string]int
func main() {
input := "/dev/stdin"
if len(os.Args) > 1 {
input = os.Args[1]
}
in, err := os.Open(input)
if err != nil {
log.Fatalf("open: %s. %s", input, err)
}
scanner := bufio.NewScanner(in)
var (
lines []Counter
maxCols, numlines int
firstline string
)
splitChar := ","
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
words := strings.Split(scanner.Text(), splitChar)
cols := len(words)
if numlines == 0 {
lines = make([]Counter, cols)
for i := range words {
lines[i] = make(Counter)
}
firstline = scanner.Text()
maxCols = cols //cap column count to first column count
}
numlines++
if cols > maxCols {
cols = maxCols
}
for i := 0; i < cols; i++ {
lines[i][words[i]]++
}
}
fmt.Printf("filename: %s\n", input)
if numlines > 0 {
fmt.Printf("firstline: %s\n", firstline)
}
for i, counter := range lines {
fmt.Printf("field#: %d distinct: %d\n", i, len(counter))
}
}
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm