Skip to content

Instantly share code, notes, and snippets.

@komuw
Last active October 29, 2020 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komuw/01d325362213076dd93bb2f95a89bfb8 to your computer and use it in GitHub Desktop.
Save komuw/01d325362213076dd93bb2f95a89bfb8 to your computer and use it in GitHub Desktop.
pointer receiver versus value receiver benchmarks
package main
import (
"fmt"
"math"
"testing"
)
/* ---------- package ---------- */
type h struct {
a int64
b map[int]string
}
type complexType struct {
//from runtime tests
q *int
w byte
e [10]byte
r []byte
t int
y uint16
u uint64
i string
h h
}
func (c complexType) complexTypeValueReceiver() string {
_ = c
return fmt.Sprint("hello")
}
func (c *complexType) complexTypePointerReceiver() string {
_ = c
return fmt.Sprint("hello")
}
/* ---------- package ---------- */
/* -------- benchmarks -------- */
var result1 string
var result2 string
func BenchmarkComplexTypeValueReceiver(b *testing.B) {
q := 67
var v complexType = complexType{
q: &q,
w: 'd',
e: [10]byte{'a', 'b', 'c', '4', '5', '6', '7', '8', '9', '0'},
r: []byte(`
Thierry Daniel Henry (French pronunciation: ​[tjɛʁi ɑ̃ʁi]; born 17 August 1977) is a French professional football coach and former player who is currently the manager of Major League Soccer club Montreal Impact.
He is considered one of the greatest strikers of all time and has often been debated as the greatest player in the history of the Premier League.[4][5][6] In 2003 and 2004, Henry was the runner-up for the FIFA World Player of the Year, and was runner-up for the Ballon d'Or in the former year.
He was named the PFA Players' Player of the Year twice, the FWA Footballer of the Year three times, and was named in the PFA Team of the Year six consecutive times.
He was also included in the FIFA FIFPro World XI once and the UEFA Team of the Year five times.
Formerly a figurehead for Nike,[7] he was one of the most commercially marketed footballers during the 2000s.
Henry made his professional debut with Monaco in 1994 before signing for defending Serie A champions Juventus.
However, limited playing time, coupled with disagreements with the club's hierarchy, led to him signing for English Premier League club Arsenal for £11 million in 1999.
Under long-time mentor and coach Arsène Wenger, Henry became a prolific striker and Arsenal's all-time leading scorer with 228 goals in all competitions.
He won the Premier League Golden Boot a record four times, won two FA Cups and two Premier League titles with the club, including one during an unbeaten season dubbed The Invincibles.
He spent his final two seasons with Arsenal as club captain, leading them to the 2006 UEFA Champions League Final.
In June 2007, he transferred to Barcelona.
In the 2008–09 season, Henry was a key part of the club's historic treble when they won La Liga, the Copa del Rey and the UEFA Champions League.
In 2010, he joined New York Red Bulls of Major League Soccer (MLS), but returned to Arsenal on loan for two months in 2012, before retiring in 2014.
Henry enjoyed sustained success with France, winning the 1998 FIFA World Cup, UEFA Euro 2000 and 2003 FIFA Confederations Cup.
He was named the French Player of the Year a record five times.
He was also named to the UEFA Euro 2000 Team of the Tournament, awarded both the 2003 FIFA Confederations Cup Golden Ball and Golden Shoe, and was named to the 2006 FIFA World Cup All-Star Team.
In October 2007, he became his country's record goalscorer.
After amassing 123 appearances and 51 goals, Henry retired from international football after the 2010 FIFA World Cup.`),
t: math.MaxInt64,
y: math.MaxUint16,
u: math.MaxUint64,
i: `
Olivier Jonathan Giroud (French pronunciation: ​[ɔlivje ʒiʁu]; born 30 September 1986) is a French professional footballer who plays as a forward for Premier League club Chelsea and the France national team.
He began his career at Grenoble in Ligue 2 before joining Tours in 2008.
In his second season at Tours, he was the division's top scorer with 21 goals, earning him a move to top-flight side Montpellier.
Giroud was again the top scorer with 21 goals in the 2011–12 season, giving the club their first ever Ligue 1 title before moving to Arsenal.
Giroud won the FA Cup with Arsenal in 2014, 2015 and 2017, and totalled 105 goals in 253 games for the club.
In January 2018, he transferred to Chelsea where, in his first full season, he became the first player from the club to score more than 10 goals in a single European campaign.
Giroud made his full international debut for France in 2011.
He has since become the nation's second-highest goalscorer with 42 goals in 100 caps, and was part of the teams which reached the quarter-finals at UEFA Euro 2012 and the 2014 FIFA World Cup,
the final of UEFA Euro 2016 (in which he received the Bronze Boot as joint second-highest goalscorer), and won the 2018 FIFA World Cup.`,
h: h{a: 90, b: map[int]string{1: "hello world", 90: "cool cool yo..."}},
}
var res string
b.ResetTimer()
for i := 0; i < b.N; i++ {
// record the result to prevent compiler eliminating the function call.
// https://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go
res = v.complexTypeValueReceiver()
}
// store the result to a package level variable
// so the compiler cannot eliminate the Benchmark itself.
result1 = res
}
func BenchmarkComplexTypePointerReceiver(b *testing.B) {
q := 67
var p complexType = complexType{
q: &q,
w: 'd',
e: [10]byte{'a', 'b', 'c', '4', '5', '6', '7', '8', '9', '0'},
r: []byte(`
Thierry Daniel Henry (French pronunciation: ​[tjɛʁi ɑ̃ʁi]; born 17 August 1977) is a French professional football coach and former player who is currently the manager of Major League Soccer club Montreal Impact.
He is considered one of the greatest strikers of all time and has often been debated as the greatest player in the history of the Premier League.[4][5][6] In 2003 and 2004, Henry was the runner-up for the FIFA World Player of the Year, and was runner-up for the Ballon d'Or in the former year.
He was named the PFA Players' Player of the Year twice, the FWA Footballer of the Year three times, and was named in the PFA Team of the Year six consecutive times.
He was also included in the FIFA FIFPro World XI once and the UEFA Team of the Year five times.
Formerly a figurehead for Nike,[7] he was one of the most commercially marketed footballers during the 2000s.
Henry made his professional debut with Monaco in 1994 before signing for defending Serie A champions Juventus.
However, limited playing time, coupled with disagreements with the club's hierarchy, led to him signing for English Premier League club Arsenal for £11 million in 1999.
Under long-time mentor and coach Arsène Wenger, Henry became a prolific striker and Arsenal's all-time leading scorer with 228 goals in all competitions.
He won the Premier League Golden Boot a record four times, won two FA Cups and two Premier League titles with the club, including one during an unbeaten season dubbed The Invincibles.
He spent his final two seasons with Arsenal as club captain, leading them to the 2006 UEFA Champions League Final.
In June 2007, he transferred to Barcelona.
In the 2008–09 season, Henry was a key part of the club's historic treble when they won La Liga, the Copa del Rey and the UEFA Champions League.
In 2010, he joined New York Red Bulls of Major League Soccer (MLS), but returned to Arsenal on loan for two months in 2012, before retiring in 2014.
Henry enjoyed sustained success with France, winning the 1998 FIFA World Cup, UEFA Euro 2000 and 2003 FIFA Confederations Cup.
He was named the French Player of the Year a record five times.
He was also named to the UEFA Euro 2000 Team of the Tournament, awarded both the 2003 FIFA Confederations Cup Golden Ball and Golden Shoe, and was named to the 2006 FIFA World Cup All-Star Team.
In October 2007, he became his country's record goalscorer.
After amassing 123 appearances and 51 goals, Henry retired from international football after the 2010 FIFA World Cup.`),
t: math.MaxInt64,
y: math.MaxUint16,
u: math.MaxUint64,
i: `
Olivier Jonathan Giroud (French pronunciation: ​[ɔlivje ʒiʁu]; born 30 September 1986) is a French professional footballer who plays as a forward for Premier League club Chelsea and the France national team.
He began his career at Grenoble in Ligue 2 before joining Tours in 2008.
In his second season at Tours, he was the division's top scorer with 21 goals, earning him a move to top-flight side Montpellier.
Giroud was again the top scorer with 21 goals in the 2011–12 season, giving the club their first ever Ligue 1 title before moving to Arsenal.
Giroud won the FA Cup with Arsenal in 2014, 2015 and 2017, and totalled 105 goals in 253 games for the club.
In January 2018, he transferred to Chelsea where, in his first full season, he became the first player from the club to score more than 10 goals in a single European campaign.
Giroud made his full international debut for France in 2011.
He has since become the nation's second-highest goalscorer with 42 goals in 100 caps, and was part of the teams which reached the quarter-finals at UEFA Euro 2012 and the 2014 FIFA World Cup,
the final of UEFA Euro 2016 (in which he received the Bronze Boot as joint second-highest goalscorer), and won the 2018 FIFA World Cup.`,
h: h{a: 90, b: map[int]string{1: "hello world", 90: "cool cool yo..."}},
}
pp := &p
var res string
b.ResetTimer()
for i := 0; i < b.N; i++ {
res = pp.complexTypePointerReceiver()
}
result2 = res
}
/* -------- benchmarks -------- */
// Pointer receivers are cheaper but not by much
/*
go test -timeout 10m -race -cover -bench=. -run=XXX ./...
goos: darwin
goarch: amd64
BenchmarkComplexTypeValueReceiver-8 772758 1419 ns/op
BenchmarkComplexTypePointerReceiver-8 838806 1302 ns/op
BenchmarkComplexTypeValueReceiver-8 794449 1328 ns/op
BenchmarkComplexTypePointerReceiver-8 907137 1310 ns/op
BenchmarkComplexTypeValueReceiver-8 794353 1339 ns/op
BenchmarkComplexTypePointerReceiver-8 922750 1347 ns/op
*/
package main
import (
"fmt"
"testing"
)
/* ---------- package ---------- */
type simpleIntType int64
func (x simpleIntType) simpleIntTypeValueReceiver() string {
_ = x
return fmt.Sprint("hello")
}
func (x *simpleIntType) simpleIntTypePointerReceiver() string {
_ = x
return fmt.Sprint("hello")
}
/* ---------- package ---------- */
/* -------- benchmarks -------- */
var result1 string
var result2 string
func BenchmarkSimpleIntTypeValueReceiver(b *testing.B) {
var v simpleIntType = 9_223_372_036_854_775_807
var res string
b.ResetTimer()
for i := 0; i < b.N; i++ {
// record the result to prevent compiler eliminating the function call.
// https://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go
res = v.simpleIntTypeValueReceiver()
}
// store the result to a package level variable
// so the compiler cannot eliminate the Benchmark itself.
result1 = res
}
func BenchmarkSimpleIntTypePointerReceiver(b *testing.B) {
var p simpleIntType = 9_223_372_036_854_775_807
pp := &p
var res string
b.ResetTimer()
for i := 0; i < b.N; i++ {
res = pp.simpleIntTypePointerReceiver()
}
result2 = res
}
/* -------- benchmarks -------- */
// Pointer receivers are cheaper but not by much
/*
go test -timeout 10m -race -cover -bench=. -run=XXX ./...
goos: darwin
goarch: amd64
BenchmarkSimpleIntTypeValueReceiver-8 802814 1399 ns/op
BenchmarkSimpleIntTypePointerReceiver-8 929792 1359 ns/op
BenchmarkSimpleIntTypeValueReceiver-8 815157 1278 ns/op
BenchmarkSimpleIntTypePointerReceiver-8 934771 1271 ns/op
BenchmarkSimpleIntTypeValueReceiver-8 801282 1311 ns/op
BenchmarkSimpleIntTypePointerReceiver-8 948777 1284 ns/op
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment