I've been using Perl for several years mostly for small to medium sized programs of sysadmim type (automation, gluing, data transformation, log searching). Recently I started to learn Go. I wanted to write something in both languages and compare. Here goes.
The Perl code is more than 2 times smaller:
$ ls -l x.* | perl -lanE 'say "$F[8]\t$F[4] bytes"' x.go 694 bytes x.pl 294 bytes
Perl code is more than 4 times slower when run ...
$ time go run x.go > /dev/null real 0m1.222s user 0m1.097s sys 0m0.220s $ time perl x.pl > /dev/null real 0m5.358s user 0m4.778s sys 0m0.497s
... and more than 5 times slower when I built the Go code:
$ go build x.go $ time ./x > /dev/null real 0m0.947s user 0m0.890s sys 0m0.126s
The code generates 10 million random integers from 0 to 9. Than it counts the occurrence of each generated integer and prints it.
$ cat x.go package main import ( "fmt" "math/rand" "time" ) func main() { // Seed the random number generator seed := rand.NewSource(time.Now().UnixNano()) r1 := rand.New(seed) // Generate random integers var ints []int for i := 0; i < 10000000; i++ { n := r1.Intn(10) ints = append(ints, n) } // Count ints occurrence count := make(map[int]int) for _, n := range ints { count[n]++ } // Sort ints var intsSorted []int for n := range count { intsSorted = append(intsSorted, n) } // Print out ints occurrence for n := range intsSorted { fmt.Printf("%d\t%d\n", n, count[n]) } } $ cat x.pl #!/usr/bin/perl use warnings; use strict; # Generate random integers my @ints; push @ints, int rand 10 for 1 .. 10_000_000; # Count ints occurrence my %count; $count{$_}++ for @ints; # Print out ints occurrence for my $int ( sort keys %count ) { printf "%d\t%d\n", $int, $count{$int}; }
In conclusion I must say that I like both languages. I like beer too :-).
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Camel vs. Gopher
by kschwab (Vicar) on Dec 08, 2018 at 21:20 UTC | |
by stevieb (Canon) on Dec 08, 2018 at 22:49 UTC | |
by kschwab (Vicar) on Dec 08, 2018 at 22:57 UTC | |
Re: Camel vs. Gopher
by Your Mother (Archbishop) on Dec 08, 2018 at 19:57 UTC | |
by marioroy (Prior) on Dec 13, 2018 at 01:25 UTC | |
by marioroy (Prior) on Dec 13, 2018 at 01:37 UTC | |
by pwagyi (Monk) on Apr 28, 2019 at 14:57 UTC | |
by Your Mother (Archbishop) on Apr 28, 2019 at 17:28 UTC | |
Re: Camel vs. Gopher
by vr (Curate) on Dec 09, 2018 at 12:45 UTC | |
Re: Camel vs. Gopher
by morgon (Priest) on Dec 12, 2018 at 09:55 UTC | |
by Your Mother (Archbishop) on Dec 12, 2018 at 19:52 UTC | |
by morgon (Priest) on Dec 12, 2018 at 21:56 UTC | |
by Your Mother (Archbishop) on Dec 12, 2018 at 23:22 UTC | |
by morgon (Priest) on Dec 12, 2018 at 23:58 UTC | |
| |
by 1nickt (Canon) on Dec 12, 2018 at 20:59 UTC | |
by marioroy (Prior) on Dec 13, 2018 at 01:49 UTC | |
by morgon (Priest) on Dec 12, 2018 at 22:00 UTC | |
by Anonymous Monk on May 21, 2024 at 08:17 UTC | |
by pwagyi (Monk) on Apr 28, 2019 at 14:52 UTC |