Giter VIP home page Giter VIP logo

go-adder's Introduction

go-adder

This is a port of the Java's LongAdder into the Go programming language.

Usage

package test

import "github.com/fangwentong/go-adder"
import "context"

var counter = &adder.LongAdder{}

func ExampleFunc(ctx context.Context)  {
    // inject goroutine probe
    var probe int32
    ctx = adder.ContextWithProbe(ctx, &probe)

    counter.Add(ctx, 2)
    counter.Add(ctx, -2)

    // counter +1
    counter.Increment(ctx)
    // counter -1
    counter.Decrement(ctx)

    // get current count
    counter.Sum()

    // reset current count
    counter.Reset()

    // get current count then reset
    counter.SumThenRest()
}

Benchmark

running on MacOS powered by Intel Coffee Lake 6 core 12 thread

$ go test -run=nothing -bench=. -cpu=1,2,4,8,16,32,64,128 -benchtime=5s
goos: darwin
goarch: amd64
pkg: github.com/fangwentong/go-adder
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkLongAdder                      691230033                8.824 ns/op
BenchmarkLongAdder-2                    687713216               18.45 ns/op
BenchmarkLongAdder-4                    477778184               12.29 ns/op
BenchmarkLongAdder-8                    1000000000               2.966 ns/op
BenchmarkLongAdder-16                   1000000000               2.573 ns/op
BenchmarkLongAdder-32                   1000000000               2.025 ns/op
BenchmarkLongAdder-64                   1000000000               2.373 ns/op
BenchmarkLongAdder-128                  1000000000               2.297 ns/op
BenchmarkLongAdderEmptyCtx              519916878               12.42 ns/op
BenchmarkLongAdderEmptyCtx-2            160806240               36.50 ns/op
BenchmarkLongAdderEmptyCtx-4            172157611               32.71 ns/op
BenchmarkLongAdderEmptyCtx-8            222932665               28.26 ns/op
BenchmarkLongAdderEmptyCtx-16           231772944               26.73 ns/op
BenchmarkLongAdderEmptyCtx-32           238532882               24.33 ns/op
BenchmarkLongAdderEmptyCtx-64           254754256               24.61 ns/op
BenchmarkLongAdderEmptyCtx-128          244789734               24.35 ns/op
BenchmarkAtomicLong                     802940997                6.566 ns/op
BenchmarkAtomicLong-2                   366768531               15.57 ns/op
BenchmarkAtomicLong-4                   388438527               15.96 ns/op
BenchmarkAtomicLong-8                   361224614               16.80 ns/op
BenchmarkAtomicLong-16                  385923369               18.04 ns/op
BenchmarkAtomicLong-32                  370266336               15.54 ns/op
BenchmarkAtomicLong-64                  387258997               16.38 ns/op
BenchmarkAtomicLong-128                 307278933               18.82 ns/op
PASS
ok      github.com/fangwentong/go-adder 177.124s

go-adder's People

Contributors

fangwentong avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.