Simpson-Computer-Technologies-Research / DeclarationSpeeds

When Declaring a Variable in Golang, should you use 'var n string' or 'n :='? Which is Faster?

Home Page:https://simpson-computer-technologies-research.github.io/DeclarationSpeeds/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Declaration Speeds Stars Watchers

banner

When Declaring a Variable in Golang, should you use 'var n string' or 'n :='? Which is Faster?

Benchmarks

PreDeclaredType() -> 333.664513ms

NoDeclaredType() -> 367.542388ms

Functions

// Pre declare the variables type
func PreDeclaredType() {
	// Declare the s variable 10^8 amount of times
	for i := 0; i < 1000000000; i++ {
		var s string = "this is a string!"
		_ = s
	}
}

// No declared variable type
func NoDeclaredType() {
	// Declare the s variable 10^8 amount of times
	for i := 0; i < 1000000000; i++ {
		s := "this is a string!"
		_ = s
	}
}

About

When Declaring a Variable in Golang, should you use 'var n string' or 'n :='? Which is Faster?

https://simpson-computer-technologies-research.github.io/DeclarationSpeeds/

License:MIT License


Languages

Language:Go 100.0%