Benchmark a call to math.Sqrt(). It is easy. Here is all it takes:

https://play.golang.org/p/1XZJsjhpIq

 

In the assembly output of “go test -v -run=Q -bench=. -gcflags=-S” you can see 
the SQRT instruction:

 

babar:sqrt mtj$ go test -v -run=Q -bench=. -gcflags=-S

# sqrt

"".BenchmarkSqrt t=1 size=59 args=0x8 locals=0x0

                0x0000 00000 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:10)       
      TEXT        "".BenchmarkSqrt(SB), $0-8

                0x0000 00000 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:10)       
      FUNCDATA              $0, gclocals·a36216b97439c93dafebe03e7f0808b5(SB)

                0x0000 00000 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:10)       
      FUNCDATA              $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)

                0x0000 00000 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:11)       
      MOVQ     "".b+8(FP), AX

                0x0005 00005 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:11)       
      MOVQ     $0, CX

                0x0007 00007 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:11)       
      MOVQ     216(AX), DX

                0x000e 00014 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:11)       
      CMPQ      CX, DX

                0x0011 00017 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:11)       
      JGE          $0, 58

                0x0013 00019 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:12)       
      MOVSD   "".A(SB), X0

                0x001b 00027 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:12)       
      SQRTSD  X0, X1

                0x001f 00031 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:12)       
       ADDSD     X1, X0

                0x0023 00035 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:12)       
      MOVSD   X0, "".A(SB)

                0x002b 00043 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:11)       
      INCQ        CX

                0x002e 00046 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:11)       
      MOVQ     216(AX), DX

                0x0035 00053 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:11)       
      CMPQ      CX, DX

                0x0038 00056 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:11)       
      JLT           $0, 19

                0x003a 00058 (/Users/mtj/gocode/src/sqrt/sqrt_test.go:14)       
      RET

 

And the output shows you the time per iteration:

 

babar:sqrt mtj$ go test -v -run=Q -bench=.

BenchmarkSqrt-8    100000000                      10.9 ns/op

PASS

ok            sqrt          1.113s

 

An empty loop is about 0.75 ns/iteration, so that means:

 

One square root requires about 10.2 nanoseconds to execute. (on my laptop 
computer)

 

In your hogcpu() function, you have a loop containing a select and a sqrt(). So 
that means:

 

If select can be implemented in 10.2 nsec, then you’ll be 50% select and 50% 
sqrt.

If 100 nsec, then you’ll be 90.7% select

if 1000 nsec, then it will be 98.99% select

:

 

there are many ways to implement a non-blocking channel, but not many that are 
faster than 10.2 nanoseconds.

 

 

From: <golang-nuts@googlegroups.com> on behalf of <vyasgiridha...@gmail.com>
Date: Monday, November 14, 2016 at 6:23 AM
To: golang-nuts <golang-nuts@googlegroups.com>
Cc: <vyasgiridha...@gmail.com>
Subject: Re: [go-nuts] Stressing the system using

 

Why would a select statement take up most of the execution time? 

 

Is there another way to implement a non blocking channel?


On Monday, November 14, 2016 at 6:01:24 PM UTC+5:30, Michael Jones wrote:

This is funny in the sense that my programs are great at stressing the CPU even 
though I don’t want to! :-) 

 

I looked at your hogcpu() function and saw an issue: you busy loop repeats a 
select statement and a square root. Almost any simple implementation of this 
will spend all of its time in the select. Put a loop around that math.Sqrt() so 
that the “hog” routine is doing mostly sqrt() and not mostly select. DO 
something similar for the hogio and other hog functions.

 

From: <golan...@googlegroups.com> on behalf of <vyasgir...@gmail.com>
Date: Monday, November 14, 2016 at 4:06 AM
To: golang-nuts <golan...@googlegroups.com>
Subject: [go-nuts] Stressing the system using

 

I have been working on this applications to stress the cpu, io and the hard 
drive.

 

https://github.com/vyasgiridhar/gstress

 

Kind of like stress. But i am unable to achieve high load. This might be due to 
the scheduling of goroutines.

Is there a work around for this?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to