Optimal data structure

Hello,
I’m working in system of record of entrances and exits of personal. The format of record is id, entrance/exit(I/O), timestamp (date + time). I’need determined what data structure is the more efficient and optimal for that. My need is access to data for calculate quantity hours of labor for id and date.
Thank’s

I would put the data into a relational db (e.g., postgesql), index the timestamp, and use sql to get the relevant data.

1 Like

In cases of structures in memory, what would be my choice?

It depends on how much data you have, what you are doing with the data, how you get the data, the hardware the program will be ran on, etc.

Forgot the most important part:

A really good definition of optimal. That is, are you optimizing for runtime, memory use, programming time, etc.

The optimal definition for this case is the use of a structure that is small and contains all the necessary information. In addition to providing access to information needed for decision making. Fundamentally I am interested in experimenting with structures in memory.

That’s a worthy goal. For this case I’d start as simple as possible.

type eventType int8

const (
	eventEntry eventType = iota
	eventExit
)

type event struct {
	userID    int
	timestamp time.Time
	eventType eventType
}

var events []event

Given that the data is entered by humans and you’re looking at, like, a couple of events per day per human in the system, there is not going to be a huge amount of data by modern computer standards. The list likely fits in memory and can be easily serialized to JSON, XML, CSV, protobufs or what have you for stable storage.

Keep the list in chronological order. Iterating over it lets you calculate entry and exit times for each user. Optionally, you can keep indexes pointing into the slice:

var userEvents map[int][]int // user ID -> slice of event indexes

Maybe keep a separate list of events per day, week, month or whatever is suitable.

Make things more complicated when you run into something that requires it - processing is too slow to answer some question (might need an index, a cache, or reorganize the structure) or doesn’t fit in memory (time to look for a real database or optimize), etc.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.