map[byte]int 和 map[string]int 具有不同的内存使用情况



这个问题来自LeetCode的一个简单问题。

这个问题与LeetCode问题本身没有太大关系。但它与两种方法有关,它们仅在map类型上有所不同,以解决这个 LeetCode 问题。


  1. 第一种方法使用map[byte]int
func romanToInt(s string) int {
m := map[byte]int{
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
result := 0
length := len(s)
last_element := length - 1
for i := 0; i < last_element; i++ {
current := m[s[i]]
next := m[s[i+1]]
if current < next {
result -= current
} else {
result += current
}
}
result += m[s[last_element]]
return result
}

LeetCode如何在线判断:

✔ Accepted
✔ 3999/3999 cases passed (16 ms)
✔ Your runtime beats 100 % of golang submissions
✔ Your memory usage beats 22 % of golang submissions (3 MB)

  1. 使用map[string]int的第二种方法:
func romanToInt(s string) int {
m := map[string]int{
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000,
}
result := 0
length := len(s)
last_element := length - 1
for i := 0; i < last_element; i++ {
current := m[string(s[i])]
next := m[string(s[i+1])]
if current < next {
result -= current
} else {
result += current
}
}
result += m[string(s[last_element])]
return result
}

LeetCode如何在线判断:

✔ Accepted
✔ 3999/3999 cases passed (16 ms)
✔ Your runtime beats 100 % of golang submissions
✔ Your memory usage beats 100 % of golang submissions (3 MB)

对在线评估的一些话: 我在 10 小时的时间间隔内运行了这两个版本超过 1 次。他们在memory usage时实现了 22% 与 100% .

我所期望的:

我认为第一个使用map[byte]int应该更快且节省内存。

为什么更快: 在第二个版本中,我每次都必须将runestring。 (但是编译器资源管理器告诉我,这并没有太大的区别。

为什么要节省内存: 因为bytestring轻.


所以最后一个问题:

为什么在memory usage有区别?
为什么我的期望是错误的?

对代码进行基准测试,romanToIntStrromanToIntBytromanToIntStrromanToIntByt之间的差异并不显着。您的代码,romanToIntStrromanToIntByt,效率不高。请参阅romanToIntArr


输出:

$ go test roman2int_test.go -bench=. -benchmem
BenchmarkRomanToIntStr-8    2725520   440 ns/op     0 B/op   0 allocs/op
BenchmarkRomanToIntByt-8    2377992   499 ns/op     0 B/op   0 allocs/op
BenchmarkRomanToIntArr-8   25643797    42.3 ns/op   0 B/op   0 allocs/op

roman2int_test.go

package main
import "testing"
func romanToIntStr(s string) int {
m := map[string]int{
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000,
}
result := 0
length := len(s)
last_element := length - 1
for i := 0; i < last_element; i++ {
current := m[string(s[i])]
next := m[string(s[i+1])]
if current < next {
result -= current
} else {
result += current
}
}
result += m[string(s[last_element])]
return result
}
func romanToIntByt(s string) int {
m := map[byte]int{
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
result := 0
length := len(s)
last_element := length - 1
for i := 0; i < last_element; i++ {
current := m[s[i]]
next := m[s[i+1]]
if current < next {
result -= current
} else {
result += current
}
}
result += m[s[last_element]]
return result
}
func romanToIntArr(s string) int {
m := [256]int{
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
result := 0
last := len(s) - 1
for i := 0; i < last; i++ {
current := m[(s[i])]
next := m[(s[i+1])]
if current < next {
result -= current
} else {
result += current
}
}
result += m[(s[last])]
return result
}
var bench1942 = "MCMXLII"
func BenchmarkRomanToIntStr(b *testing.B) {
for N := 0; N < b.N; N++ {
romanToIntStr(bench1942)
}
}
func BenchmarkRomanToIntByt(b *testing.B) {
for N := 0; N < b.N; N++ {
romanToIntByt(bench1942)
}
}
func BenchmarkRomanToIntArr(b *testing.B) {
for N := 0; N < b.N; N++ {
romanToIntArr(bench1942)
}
}

相关内容

最新更新