Dictionary map[byte]int和map[string]int有不同的内存使用
这个问题来自中国 这个问题与LeetCode问题本身没有太大关系。但它与两种方法相关,这两种方法仅在Dictionary map[byte]int和map[string]int有不同的内存使用,dictionary,go,memory-management,Dictionary,Go,Memory Management,这个问题来自中国 这个问题与LeetCode问题本身没有太大关系。但它与两种方法相关,这两种方法仅在map类型上有所不同,用于解决LeetCode问题。 使用map[byte]int的第一种方法: 第二种方法是使用map[string]int: 对在线评估有一些建议: 我已经在1小时的时间间隔内运行了这两个版本超过10次。在内存使用率方面,它们分别达到22%和100% 我的预期: 我认为第一个使用map[byte]int的应该更快,节省内存 为什么更快: 在第二个版本中,我每次都必须将符文
map
类型上有所不同,用于解决LeetCode问题。
map[byte]int
的第一种方法:map[string]int
:对在线评估有一些建议: 我已经在1小时的时间间隔内运行了这两个版本超过10次。在
内存使用率方面,它们分别达到22%和100%
我的预期:
我认为第一个使用map[byte]int
的应该更快,节省内存
为什么更快:
在第二个版本中,我每次都必须将符文
转换为字符串
。
(但报告告诉我,这并没有太大区别。)
为什么要节省内存:
因为字节
比字符串
更轻
最后一个问题:
为什么在内存使用方面存在差异
为什么我的期望是错误的?基准测试你的代码,romanToIntStr
和romanToIntByt
。romantintstr
和romantintbyt
之间的差异不显著。您的代码,romanToIntStr
和romanToIntByt
效率低下。请参见RomantoIntar
输出:
$ 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\u测试开始
:
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)
}
}
主程序包
导入“测试”
func romanToIntStr(s字符串)int{
m:=map[string]int{
"一":,
“V”:5,
“X”:10,
“L”:50,
“C”:100,
“D”:500,
“M”:1000,
}
结果:=0
长度:=长度(s)
最后一个元素:=长度-1
对于i:=0;i
对代码进行基准测试,romanToIntStr
和romanToIntByt
。romantintstr
和romantintbyt
之间的差异不显著。您的代码,romanToIntStr
和romanToIntByt
效率低下。请参见RomantoIntar
输出:
$ 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\u测试开始
:
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)
}
}
主程序包
导入“测试”
func romanToIntStr(s字符串)int{
m:=map[string]int{
"一":,
“V”:5,
“X”:10,
“L”:50,
“C”:100,
“D”:500,
“M”:1000,
}
结果:=0
长度:=长度(s)
最后一个元素:=长度-1
对于i:=0;i
首先,这两个结果都表示为16ms和3MB,因此实际上可能没有什么区别(您必须运行适当的基准测试)。将一条线转换为一条线
$ 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
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)
}
}