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

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的应该更快,节省内存 为什么更快: 在第二个版本中,我每次都必须将符文

这个问题来自中国

这个问题与LeetCode问题本身没有太大关系。但它与两种方法相关,这两种方法仅在
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)
        }
    }