How Expensive Is a Go Function Call?
I was recently benchmarking various implementations of an algorithm and noticed that the recursive implementation of an algorithm performed worse than its inline equivalent. I didn’t know if it made sense to attribute this overhead to the cost of the additional function calls in the recursive implementation. I set out to see if I could see behind the scenes of a Go function call and determine just how expensive each function call is. TLDR: The cost of a function call? I still don’t know. The best I can offer is that it depends, well that and an exploration of Go assembler. In this post, I take you through the analysis I did and highlight what I learned along the way. Rather than use a complex algorithm, I sought to use the simplest possible program that demonstrated the behaviour I wanted to investigate. This Go program takes an int, in this case 1000, and increments it by 1 inside a loop that executes 1000 iterations. The result, 2000, is printed to the screen. I included the loop to
I was recently benchmarking various implementations of an algorithm and noticed that the recursive implementation of an algorithm performed worse than its inline equivalent. I didn't know if it made sense to attribute this overhead to the cost of the additional function calls in the recursive implementation. I set out to see if I could see behind the scenes of a Go function call and determine just how expensive each function call is. TLDR: The cost of a function call? I still don't know. The best I can offer is that it depends, well that and an exploration of Go assembler. In this post, I take
Explore this link on the map →related reading
- abseil / Performance Hintsabseil.io
- Infographics: Operation Costs in CPU Clock Cycles - 6IT6it.dev
- CS107 Lab 7: Optimizing, Profiling, and Ethicsweb.stanford.edu
- Frequently Asked Questions (FAQ) - The Go Programming Languagego.dev
- Compilers - What Every Programmer Should Know About Compiler Optimizations | Microsoft Learnlearn.microsoft.com
- From hand-tuned Go to self-optimizing code: Building BitsEvolve | Datadogdatadoghq.com
- Notes on structured concurrency, or: Go statement considered harmful - njs blogvorpus.org
- Introduction to reverse engineering and Assembly. | KaKaRoTo's Blogkakaroto.homelinux.net
- Function Lengthmartinfowler.com
- abseil / Performance Hintsabseil.io
- Four Days of Go – Evan Millerevanmiller.org
- Diagnostics - The Go Programming Languagego.dev