The Computer Language
24.04 Benchmarks Game

simple Go #2 program

source code

/* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
 *
 * contributed by The Go Authors.
 * Based on mandelbrot.c contributed by Greg Buchholz
*/

package main

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

var n = 0

func main() {
   flag.Parse()
   if flag.NArg() > 0 { n,_ = strconv.Atoi( flag.Arg(0) ) }

   out := bufio.NewWriter(os.Stdout)
   defer out.Flush()

   w := n
   h := n
   bit_num := 0
   byte_acc := byte(0)
   const Iter = 50
   const Zero float64 = 0
   const Limit = 2.0

   fmt.Fprintf(out, "P4\n%d %d\n", w, h)

   for y := 0; y < h; y++ {
      for x := 0; x<w; x++ {
         Zr, Zi, Tr, Ti := Zero, Zero, Zero, Zero
         Cr := (2*float64(x)/float64(w) - 1.5)
         Ci := (2*float64(y)/float64(h) - 1.0)

         for i := 0; i < Iter && (Tr+Ti <= Limit*Limit); i++ {
            Zi = 2*Zr*Zi + Ci
            Zr = Tr - Ti + Cr
            Tr = Zr * Zr
            Ti = Zi * Zi
         }

         byte_acc <<= 1
         if Tr+Ti <= Limit*Limit {
            byte_acc |= 0x01
         }

         bit_num++

         if bit_num == 8 {
            out.WriteByte(byte_acc)
            byte_acc = 0
            bit_num = 0
         } else if x == w-1 {
            byte_acc <<= uint(8-w%8)
            out.WriteByte(byte_acc)
            byte_acc = 0
            bit_num = 0
         }
      }
   }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
go version go1.22.0 linux/amd64
GOAMD64=v2


 Fri, 01 Mar 2024 23:57:27 GMT

MAKE:
/opt/src/go1.22.0/go/bin/go build -o simple.go-2.go_run simple.go-2.go

5.19s to complete and log all make actions

COMMAND LINE:
 ./simple.go-2.go_run 16000

(BINARY) PROGRAM OUTPUT NOT SHOWN