登录
首页 >  Golang >  Go问答

选取最佳数据结构以提高“日历”API性能

来源:stackoverflow

时间:2024-03-03 10:00:26 114浏览 收藏

你在学习Golang相关的知识吗?本文《选取最佳数据结构以提高“日历”API性能》,主要介绍的内容就涉及到,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!

问题内容

我有一个微服务,为员工的工作日历提供服务。原始数据的文件结构如下:

calendars / [year] / [employee].json

[employee].json 的内容:

{
  // [date]: [hours]
  "2023-02-01": 7,
  "2023-02-02": 7,
}

使用 go,我将所有数据转换为这两个类型为 calendarfastcalendarslow 的变量:

type username = string
type date = string
type year = string
type hours = float64

type calendarfast map[username]map[year][]struct {
    time  time.time // "2022-01-01"
    hours hours // 7
}

type calendarslow map[username]map[year]map[date]hours

例如,如果我想从我的 api 返回给定日期范围内的所有小时,我可以编写这两个基准来测试哪种数据结构更好:

func benchmarknew(b *testing.b) {
    c := make(calendarfast)
    c.update(context.todo())

    from, _ := time.parse("2006-01-02", "2023-01-01")
    to, _ := time.parse("2006-01-02", "2023-02-01")

    isinrange := func(t, from, to time.time) bool {
        return (t.after(from) && t.before(to)) || t.equal(from) || t.equal(to)
    }

    b.resettimer()

    for i := 0; i < b.n; i++ {
        // result is map[username]map[date]hours
        result := make(map[string]map[string]float64)
        for username, years := range c {
            if _, ok := result[username]; !ok {
                result[username] = make(map[string]float64)
            }

            for _, dates := range years {
                for i, n := 0, len(dates); i < n; i++ {
                    if isinrange(dates[i].time, from, to) {
                        result[username][dates[i].time.string()] = dates[i].hours
                    }
                }
            }
        }
    }
}

func benchmarkold(b *testing.b) {
    c := make(calendarslow)
    c.update(context.todo())

    from, _ := time.parse("2006-01-02", "2023-01-01")
    to, _ := time.parse("2006-01-02", "2023-02-01")

    b.resettimer()
    for i := 0; i < b.n; i++ {
        result := make(map[string]map[string]float64)
        for username, years := range c {
            if _, ok := result[username]; !ok {
                result[username] = make(map[string]float64)
            }

            for _, items := range years {
                for date, hours := range items {
                    t, err := time.parse("2006-01-02", date)
                    if err != nil {
                        continue
                    }

                    if (t.after(from) && t.before(to)) || t.equal(from) || t.equal(to) {
                        result[username][date] = hours
                    }
                }
            }
        }
    }
}

我有以下基准,这表明在数组中搜索范围要快得多,搜索映射中的每个元素:

benchmarknew-4  285    4208045 ns/op  692965 b/op  6303 allocs/op
benchmarkold-4   39   29672935 ns/op  621877 b/op  1407 allocs/op

我的问题是:如何进一步提高性能?我应该将数据结构更改为另一个吗?

我需要速度,因为我收到了很多对当前 php api 的调用,并发现 go 中的微服务可以轻松处理 1k rps,而 k8s 中仅需要 10m cpu 和 64mib 内存

我已经对“time.time”方法的数组感到满意,但对其他实现感到好奇。

更新:

test iterations time
Mine with array 229 5259548 ns/op
erik258 414 2892932 ns/op

正确答案


根据您的描述,数据是静态的,您仅在程序启动时读取源文件一次。

为什么不对每个员工的日期数组进行排序,以便一旦超过结束日期,您就可以停止检查其余日期?

为什么会有year地图存在?还需要迭代一件事。

为什么将所有日期存储为字符串,然后每次要按日期范围过滤时都必须解析它们?为什么不使用 time.time ?内存效率更高,并且无需一遍又一遍地解析。

我会使用 map[username][]struct{date time.time, hours int},在读取一次后对数组进行排序,然后像这样迭代:

for username, dates := range c {
   for shift := range dates {
      if shift.date.Before(from) {
         continue
      } else if shift.date.After(to) {
         break
      }
      if _, ok := result[username]; !ok {
         result[username] = make(map[string]float64)
      }
      result[username][shift.date] = shift.hours
   }
}

今天关于《选取最佳数据结构以提高“日历”API性能》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>