提高搜索结构体切片中特定值的性能

huangapple go评论79阅读模式
英文:

Improving performance of searching slice of structs for value

问题

如何优化下面的代码以搜索一个包含映射的数组,以查找特定的键值对(然后返回其他键值对)?

type userMap struct {
    JiraUsername string
    CHProjectID  int
    CHID         string
}

func main() {
    var userMaps []userMap

    userMaps = append(userMaps, userMap{
        JiraUsername: "ted",
        CHProjectID:  81,
        CHID:         "23jk3f32jl3323",
    })

    fmt.Println(GetUserInfo(userMaps, "ted"))
}

func GetUserInfo(userMaps []userMap, jiraUsername string) (CHProjectID int, CHID string) {
    for _, u := range userMaps {
        if u.JiraUsername == jiraUsername {
            return u.CHProjectID, u.CHID
        }
    }
    return 0, ""
}

代码已经很简洁了,但如果你想进一步优化,可以考虑以下几点:

  1. 使用 map[string]userMap 替代数组来存储用户映射,这样可以通过用户名直接访问对应的映射,而无需遍历整个数组。
  2. GetUserInfo 函数中,使用 ok 来判断是否找到了匹配的用户映射,而不是使用循环遍历。
  3. 如果你需要返回多个键值对,可以将它们封装在一个结构体中返回,而不是分别返回。

下面是优化后的代码示例:

type userMap struct {
    JiraUsername string
    CHProjectID  int
    CHID         string
}

func main() {
    userMaps := make(map[string]userMap)

    userMaps["ted"] = userMap{
        JiraUsername: "ted",
        CHProjectID:  81,
        CHID:         "23jk3f32jl3323",
    }

    fmt.Println(GetUserInfo(userMaps, "ted"))
}

type UserInfo struct {
    CHProjectID int
    CHID        string
}

func GetUserInfo(userMaps map[string]userMap, jiraUsername string) (UserInfo, bool) {
    u, ok := userMaps[jiraUsername]
    if ok {
        return UserInfo{
            CHProjectID: u.CHProjectID,
            CHID:        u.CHID,
        }, true
    }
    return UserInfo{}, false
}

希望对你有帮助!如果有任何其他问题,请随时提问。

英文:

How can I optimize the code below for searching an array of maps for a specific key-value (and then return the other key-values)?

type userMap struct {
	JiraUsername	string
	CHProjectID	int
	CHID		string
}

func main() {
	var userMaps []userMap
	
	userMaps = append(userMaps, userMap{
		JiraUsername: "ted",
		CHProjectID: 81,
		CHID: "23jk3f32jl3323",
	})
	
	fmt.Println(GetUserInfo(userMaps, "ted"))
}

func GetUserInfo(userMaps []userMap, jiraUsername string) (CHProjectID int, CHID string) {
	for _, u := range userMaps {
		if u.JiraUsername == jiraUsername {
			return u.CHProjectID, u.CHID
		}
	}
	return 0, ""
}

答案1

得分: 1

你的代码是合理的,但它不是数组的映射,而是结构体的切片。更高效的方法是:

type userMap struct {
    JiraUsername string
    CHProjectID  int
    CHID         string
}
type UsersMap map[string]userMap

func main() {
    userMaps := make(UsersMap)
    userMaps["ted"] = userMap{
        JiraUsername: "ted",
        CHProjectID:  81,
        CHID:         "23jk3f32jl3323",
    }

    fmt.Println(GetUserInfo(userMaps, "ted"))
}

func GetUserInfo(userMaps UsersMap, jiraUsername string) (CHProjectID int, CHID string) {
    if u, ok := userMaps[jiraUsername]; ok {
        return u.CHProjectID, u.CHID
    }
    return 0, ""
}

这种方法不需要循环,时间复杂度为 O(1)。

英文:

Your code is reasonable, but it's not array of map, it's slice of struct. The efficient way would be

type userMap struct {
	JiraUsername string
	CHProjectID  int
	CHID         string
}
type UsersMap map[string]userMap

func main() {
	userMaps := make(UsersMap)
	userMaps["ted"] = userMap{
		JiraUsername: "ted",
		CHProjectID:  81,
		CHID:         "23jk3f32jl3323",
	}

	fmt.Println(GetUserInfo(userMaps, "ted"))
}

func GetUserInfo(userMaps UsersMap, jiraUsername string) (CHProjectID int, CHID string) {
	if u, ok := userMaps[jiraUsername]; ok {
		return u.CHProjectID, u.CHID
	}
	return 0, ""
}

This doesn't require loop and has O(1) complexity.

huangapple
  • 本文由 发表于 2016年9月8日 23:46:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/39395279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定