gpt4 book ai didi

dictionary - map[byte]int 和 map[string]int 有不同的内存使用

转载 作者:IT王子 更新时间:2023-10-29 02:35:25 25 4
gpt4 key购买 nike

本题来自a little simple problem from LeetCode .

这个问题与 LeetCode 问题本身没有太大关系。但是它与解决这个 LeetCode 问题的两种方法有关,它们只是 map 的类型不同。


  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)

在线评价的一些话:我在 1 小时的时间间隔内运行了这两个版本超过 10 次。在内存使用时,它们分别达到了 22% 和 100%。

我的预期:

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

为什么更快:在第二个版本中,我每次都必须将 rune 转换为 string。(但是 compiler explorer 告诉我这没什么大的区别。)

为什么要节省内存:因为 bytestring 更轻量。


最后一个问题:

为什么内存使用有差异?
为什么我的期望是错误的?

最佳答案

对您的代码进行基准测试,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)
}
}

关于dictionary - map[byte]int 和 map[string]int 有不同的内存使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55550559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com