Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // go build benchmark.go && time ./benchmark
- package main
- import (
- "fmt"
- "math/rand"
- )
- type Player struct {
- hp int
- }
- func NewPlayer() Player {
- return Player{hp: 100}
- }
- type Cell struct {
- x int
- y int
- item *Player
- }
- type World struct {
- width int
- height int
- grid [][]Cell
- }
- func NewWorld(width, height int) World {
- fmt.Println("creating a grid with width=", width, " height=", height)
- grid := make([][]Cell, height)
- for y := 0; y < height; y++ {
- row := make([]Cell, width)
- for x := 0; x < width; x++ {
- row[x] = Cell{x, y, nil}
- }
- grid[y] = row
- }
- return World{width, height, grid}
- }
- func (world *World) CountPlayers() int {
- n := 0
- for _, row := range world.grid {
- for _, cell := range row {
- if cell.item != nil {
- n++
- }
- }
- }
- return n
- }
- func (world *World) SpawnPlayers(count int) {
- fmt.Println("spawning ", count, " players")
- for i := 0; i < count; i++ {
- x := rand.Intn(world.width)
- y := rand.Intn(world.height)
- player := NewPlayer()
- world.grid[y][x].item = &player
- }
- }
- func main() {
- N := 10_000
- ROUNDS := 10
- for i := 0; i < ROUNDS; i++ {
- fmt.Println("round ", i+1)
- world := NewWorld(N, N)
- world.SpawnPlayers(N * 100)
- fmt.Println(world.CountPlayers())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement