Performance Evaluation: Slicing and Dicing with and without Capacity Hints

Performance Evaluation: Slicing and Dicing with and without Capacity Hints

In Go, the make function is used to create slices, maps, and channels with a specified capacity. When initialising slices with make, providing a capacity hint can be beneficial for performance, especially when you know the approximate size of the data.

// Creating a slice with a capacity hint of 10
mySlice := make([]int, 0, 10)        

Unlike maps, slice capacity is not a hint: the compiler will allocate enough memory for the capacity of the slice as provided to make(), which means that subsequent append() operations will incur zero allocations (until the length of the slice matches the capacity, after which any appends will require a resize to hold additional elements).

Example


package awesomeProject2

import (
    "testing"
)

// BenchmarkBadCapacityHint benchmarks appending to a slice without a good capacity hint.
func BenchmarkBadCapacityHint(b *testing.B) {
    for i := 0; i < b.N; i++ {
       var badSlice []int

       for j := 0; j < 1000; j++ {
          badSlice = append(badSlice, j)
       }
    }
}

// BenchmarkGoodCapacityHint benchmarks appending to a slice with a good capacity hint.
func BenchmarkGoodCapacityHint(b *testing.B) {
    for i := 0; i < b.N; i++ {
       goodSlice := make([]int, 0, 1000)

       for j := 0; j < 1000; j++ {
          goodSlice = append(goodSlice, j)
       }
    }
}        

Output:

results of capacity

We can clearly see the difference between passing the capacity while creating slice and vice versa.

Venukishore R

GoLang developer | Node.js developer | Backend developer

1 年

Thank you na

回复

要查看或添加评论,请登录

Sudhakar Annadurai的更多文章

  • Contexts in Go: Best Practices

    Contexts in Go: Best Practices

    The package in Go serves as a powerful tool for managing the flow of data and deadlines across functions, providing a…

    1 条评论
  • Exploring the Evolution of Loop Scoping

    Exploring the Evolution of Loop Scoping

    Go programming, known for its simplicity and robustness, is set to undergo a significant change in the way loop…

    1 条评论
  • What's GOB?

    What's GOB?

    GOB, short for "Go Binary," is a package in Go that helps in turning our fancy Go data structures—like structs, slices,…

    1 条评论
  • A Beginner's Guide to Containerization

    A Beginner's Guide to Containerization

    Are you intrigued by Docker but not sure where to begin? Let's dive into the world of Docker using simple commands to…

    1 条评论
  • Implementing a Batch Processor

    Implementing a Batch Processor

    In today's computing landscape, efficiently processing large volumes of data is crucial. One effective approach to…

  • Building a Simple Ledger System in Golang

    Building a Simple Ledger System in Golang

    In this article, we'll create a basic ledger system using Golang. The ledger will be capable of recording transactions,…

  • Implementing Authentication and Authorization in Go

    Implementing Authentication and Authorization in Go

    Security is a critical aspect of any web application. Implementing robust authentication and authorization mechanisms…

    1 条评论
  • Understanding Printing in Windows

    Understanding Printing in Windows

    Printing documents in a Windows environment may seem like a straightforward task, but behind the scenes, it involves a…

  • The Evolution of Large Language Models: Towards Self-Hosting and Accessibility

    The Evolution of Large Language Models: Towards Self-Hosting and Accessibility

    In the realm of large language models (LLMs), there’s a remarkable shift underway - an endeavor to make these powerful…

  • Making HTTP GET Requests in Go

    Making HTTP GET Requests in Go

    Introduction: In modern web development, interacting with external APIs is a common task. In Go, the net/http package…

社区洞察

其他会员也浏览了