github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/zerostream/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/egonelbre/exp/zerostream/op"
     8  
     9  	"github.com/pkg/profile"
    10  )
    11  
    12  type Rect struct{ X, Y, W, H int32 }
    13  
    14  func (r Rect) Draw(out *op.Iterator) {
    15  	out.Start()
    16  	*out.MoveTo() = op.MoveTo{r.X, r.Y}
    17  	*out.LineTo() = op.LineTo{r.X + r.W, r.Y}
    18  	*out.LineTo() = op.LineTo{r.X + r.W, r.Y + r.H}
    19  	*out.LineTo() = op.LineTo{r.X, r.Y + r.H}
    20  	out.Close()
    21  }
    22  
    23  func (r Rect) Render(out *op.Iterator) {
    24  	r.Draw(out)
    25  	out.Fill()
    26  	out.Stroke()
    27  }
    28  
    29  func main() {
    30  	x := op.NewStream(1 << 30)
    31  	it := x.Iterate()
    32  
    33  	defer profile.Start(&profile.Config{
    34  		CPUProfile:  true,
    35  		ProfilePath: ".",
    36  	}).Stop()
    37  
    38  	start := time.Now()
    39  	for i := 0; i < 1<<20; i += 1 {
    40  		*it.Translate() = op.Translate{2, 2}
    41  		Rect{10, 10, 20, 20}.Render(it)
    42  	}
    43  	fmt.Println(time.Since(start))
    44  	fmt.Println("Last ", it.Head)
    45  
    46  	start = time.Now()
    47  	it = x.Iterate()
    48  	z := int32(0)
    49  
    50  RENDER:
    51  	for {
    52  		switch it.Type() {
    53  		case op.EOF:
    54  			break RENDER
    55  		case op.TypeStart:
    56  			it.Start()
    57  		case op.TypeClose:
    58  			it.Close()
    59  		case op.TypeMoveTo:
    60  			z += it.MoveTo().X
    61  		case op.TypeLineTo:
    62  			z += it.LineTo().X
    63  		case op.TypeTranslate:
    64  			z += it.Translate().Dx
    65  		case op.TypeFill:
    66  			it.Fill()
    67  		case op.TypeStroke:
    68  			it.Stroke()
    69  		default:
    70  			panic("unhandled type")
    71  		}
    72  	}
    73  	fmt.Println(time.Since(start))
    74  	fmt.Println("Last ", it.Head, z)
    75  }