Code Monkey home page Code Monkey logo

go-labs's Issues

链表遍历可能需要考虑cache miss

测试环境的数据分布在连续内存中:

func init() {
        for i := 0; i < 1000; i++ {
                var newData = new(BigStruct)
                newData.C30 = i
                newData.next = data
                data = newData
        }
}

真实环境中,玩家数据在内存中的分布可能不是连续的

func init_cache_miss() {
        for i := 0; i < 1800; i++ {
                var newData = new(BigStruct)
                if i%5 >= 3 {
                        newData.C30 = i
                        newData.next = otherUserData
                        otherUserData = newData
                } else {
                        newData.C30 = i
                        newData.next = data
                        data = newData
                }
        }
}

原始版本:

PASS
Benchmark_Loop1-8     300000          5156 ns/op
Benchmark_Loop2-8     200000          5891 ns/op
Benchmark_Loop3-8     100000         14506 ns/op
Benchmark_Loop4-8      20000         95709 ns/op
Benchmark_Loop5-8       3000        447557 ns/op
Benchmark_Loop6-8     200000          8395 ns/op
Benchmark_Loop7-8     200000          9301 ns/op
ok      go-labs/labs07  12.440s

模拟 cache miss 情况

PASS
Benchmark_Loop1-8     200000          9960 ns/op
Benchmark_Loop2-8     200000         10466 ns/op
Benchmark_Loop3-8     100000         18221 ns/op
Benchmark_Loop4-8      10000        107809 ns/op
Benchmark_Loop5-8       3000        507910 ns/op
Benchmark_Loop6-8     200000         11533 ns/op
Benchmark_Loop7-8     200000         11954 ns/op
ok      go-labs/labs07  13.934s

我觉得还是 slice 效率更优。因为 slice 可以保证同一个玩家的数据在一段连续内存。

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.