Source file src/simd/archsimd/_gen/simdgen/bench.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"os"
    11  	"runtime"
    12  	"time"
    13  )
    14  
    15  // startBench starts measuring simdgen for -bench. It returns a function that
    16  // prints the measurements to stderr as a Go benchmark result line, so the
    17  // output of several runs can be compared with benchstat.
    18  func startBench() func() {
    19  	start := time.Now()
    20  	return func() {
    21  		wall := time.Since(start)
    22  		var ms runtime.MemStats
    23  		runtime.ReadMemStats(&ms)
    24  
    25  		var buf bytes.Buffer
    26  		fmt.Fprintf(&buf, "BenchmarkSimdgen/target=%s", *FlagArch)
    27  		// Like the testing package, add a -GOMAXPROCS suffix unless it's 1.
    28  		if procs := runtime.GOMAXPROCS(0); procs != 1 {
    29  			fmt.Fprintf(&buf, "-%d", procs)
    30  		}
    31  		fmt.Fprintf(&buf, " 1 %d ns/op", wall.Nanoseconds())
    32  		if t, ok := cpuTime(); ok {
    33  			fmt.Fprintf(&buf, " %d cpu-ns/op", t.Nanoseconds())
    34  		}
    35  		fmt.Fprintf(&buf, " %d B/op %d allocs/op\n", ms.TotalAlloc, ms.Mallocs)
    36  		os.Stderr.Write(buf.Bytes())
    37  	}
    38  }
    39  

View as plain text