Here is how it looks generally:
以下是它的外观:
I get values from an outside device and I have to store a timestamp with them. Somehow I want to store the info in an efficient way to make my life easier when it comes to processing all the stuff.
我从外部设备获取值,我必须存储时间戳。不知何故,我想以有效的方式存储信息,以便在处理所有内容时让我的生活更轻松。
As a simple solution I have created a class which stores one row:
作为一个简单的解决方案,我创建了一个存储一行的类:
public class X
{
public DateTime timestamp;
public double value1;
public double value2;
}
And then I create a List out of these objects.
然后我从这些对象中创建一个List。
Can you show a better way of dealing with this kind of data?
你能展示一种处理这类数据的更好方法吗?
Thanks!
4 个解决方案
#1
That seems perfectly reasonable to me, although I'd use properties rather than public fields. If you're fetching values from an external device, you may want to consider making the type immutable - pass all the values into the constructor and store them in readonly fields. I find it easier to reason about my code when the types are immutable :)
这对我来说似乎是完全合理的,虽然我使用属性而不是公共字段。如果从外部设备获取值,则可能需要考虑使类型为immutable - 将所有值传递给构造函数并将它们存储在只读字段中。当类型不可变时,我发现更容易推理我的代码:)
What sort of efficiency are you concerned about? How many of these values will you have? Even though there aren't many values here, I'd probably still use a class instead of a struct... I very rarely create my own structs. Again, I just find classes easier to reason about (no need to worry about boxing, for example.) Also, this feels like "a collection of values" rather than one indivisible value which is what structs are usually used for (numbers etc). It just doesn't feel like a struct to me.
你关注什么样的效率?你有多少这些价值观?即使这里没有很多值,我可能仍然使用类而不是结构...我很少创建自己的结构。再一次,我只是发现类更容易推理(例如,不需要担心拳击。)此外,这感觉就像“一组值”而不是一个不可分割的值,这是结构通常用于(数字等) 。它对我来说感觉不像是一个结构。
You will incur an overhead per object, but I wouldn't worry about that unless you have a really good reason to.
每个对象会产生一个开销,但除非你有充分的理由,否则我不会担心。
EDIT: This seems a trivial point to make, but give everything meaningful names. For example:
编辑:这似乎是一个微不足道的要点,但给出一切有意义的名字。例如:
public class DeviceReading
{
private readonly DateTime timeStamp;
public DateTime TimeStamp { get { return timeStamp; } }
private readonly double temperature;
public double Temperature { get { return temperature; } }
private readonly double airPressure;
public double AirPressure { get { return airPressure; } }
public DeviceReading (DateTime timeStamp,
double temperature,
double airPressure)
{
this.timeStamp = timeStamp;
this.temperature = temperature;
this.airPressure = airPressure;
}
}
I suspect you'd do so anyway, but I just thought I'd make sure :)
我怀疑你无论如何都会这样做,但我只是觉得我确定:)
A couple of other things to consider:
还有几件需要考虑的事情:
-
Assuming you're using a fairly recent version of .NET, you might want to use
DateTimeOffset
rather thanDateTime
. The benefit is thatDateTiemOffset
unambiguously represents an instant in time, whereasDateTime
can be UTC, local or unspecified.假设您使用的是最新版本的.NET,您可能希望使用DateTimeOffset而不是DateTime。好处是DateTiemOffset明确地表示即时,而DateTime可以是UTC,本地或未指定。
-
Depending on the kind of readings you're taking (and how they're communicated) you may want to use
decimal
instead ofdouble
. Probably not for temperature and air pressure (or any other "physical" values) but if you're dealing with artificial values like money,decimal
is your friend. Given that this is reading from a device, you're probably dealing with physical quantities, but I thought it worth mentioning.根据您正在读取的读数类型(以及它们如何通信),您可能希望使用十进制而不是双精度。可能不是温度和气压(或任何其他“物理”值),但如果你正在处理金钱这样的人为价值,小数就是你的朋友。鉴于这是从设备读取,你可能正在处理物理量,但我认为值得一提。
-
For dealing with the collections of readings, you really want to look into LINQ. It's lovely :)
为了处理读数集合,你真的想看看LINQ。真可爱:)
#2
This type is small enough that, assuming it is immutable, you may like to consider using a struct. This will reduce the load on the garbage collected heap.
这种类型足够小,假设它是不可变的,您可能会考虑使用结构。这将减少垃圾收集堆上的负载。
What exactly are you trying to optimise? Memory footprint? Read performance? How will you be reading the data?
您究竟想要优化什么?内存占用?阅读表现?你将如何阅读数据?
#3
If you plan to put many X
object in an array, I suggest making it a struct
since it holds just a few "plain old data" items (no objects). Other than that, it seems like a straightforward way to work with the data.
如果你计划将许多X对象放在一个数组中,我建议将它作为一个结构,因为它只包含一些“普通的旧数据”项(没有对象)。除此之外,它似乎是一种简单的数据处理方式。
#4
There is nothing particularly wrong with the way you are already doing it. (Although as others have suggested, you should be making your fields private and using public properties instead)
你已经这样做的方式没有什么特别的错误。 (尽管正如其他人所建议的那样,你应该将你的字段设为私有并改为使用公共属性)
Often the best way to decide upon what storage structure to use is to consider how you will be using the data.
通常,决定使用哪种存储结构的最佳方法是考虑如何使用数据。
For example, if you know you need to process the data in some kind of order, consider a SortedList instead. If you know you need direct access to the data from some kind of key or index, use a Dictionary.
例如,如果您知道需要以某种顺序处理数据,请考虑使用SortedList。如果您知道需要从某种键或索引直接访问数据,请使用词典。
#1
That seems perfectly reasonable to me, although I'd use properties rather than public fields. If you're fetching values from an external device, you may want to consider making the type immutable - pass all the values into the constructor and store them in readonly fields. I find it easier to reason about my code when the types are immutable :)
这对我来说似乎是完全合理的,虽然我使用属性而不是公共字段。如果从外部设备获取值,则可能需要考虑使类型为immutable - 将所有值传递给构造函数并将它们存储在只读字段中。当类型不可变时,我发现更容易推理我的代码:)
What sort of efficiency are you concerned about? How many of these values will you have? Even though there aren't many values here, I'd probably still use a class instead of a struct... I very rarely create my own structs. Again, I just find classes easier to reason about (no need to worry about boxing, for example.) Also, this feels like "a collection of values" rather than one indivisible value which is what structs are usually used for (numbers etc). It just doesn't feel like a struct to me.
你关注什么样的效率?你有多少这些价值观?即使这里没有很多值,我可能仍然使用类而不是结构...我很少创建自己的结构。再一次,我只是发现类更容易推理(例如,不需要担心拳击。)此外,这感觉就像“一组值”而不是一个不可分割的值,这是结构通常用于(数字等) 。它对我来说感觉不像是一个结构。
You will incur an overhead per object, but I wouldn't worry about that unless you have a really good reason to.
每个对象会产生一个开销,但除非你有充分的理由,否则我不会担心。
EDIT: This seems a trivial point to make, but give everything meaningful names. For example:
编辑:这似乎是一个微不足道的要点,但给出一切有意义的名字。例如:
public class DeviceReading
{
private readonly DateTime timeStamp;
public DateTime TimeStamp { get { return timeStamp; } }
private readonly double temperature;
public double Temperature { get { return temperature; } }
private readonly double airPressure;
public double AirPressure { get { return airPressure; } }
public DeviceReading (DateTime timeStamp,
double temperature,
double airPressure)
{
this.timeStamp = timeStamp;
this.temperature = temperature;
this.airPressure = airPressure;
}
}
I suspect you'd do so anyway, but I just thought I'd make sure :)
我怀疑你无论如何都会这样做,但我只是觉得我确定:)
A couple of other things to consider:
还有几件需要考虑的事情:
-
Assuming you're using a fairly recent version of .NET, you might want to use
DateTimeOffset
rather thanDateTime
. The benefit is thatDateTiemOffset
unambiguously represents an instant in time, whereasDateTime
can be UTC, local or unspecified.假设您使用的是最新版本的.NET,您可能希望使用DateTimeOffset而不是DateTime。好处是DateTiemOffset明确地表示即时,而DateTime可以是UTC,本地或未指定。
-
Depending on the kind of readings you're taking (and how they're communicated) you may want to use
decimal
instead ofdouble
. Probably not for temperature and air pressure (or any other "physical" values) but if you're dealing with artificial values like money,decimal
is your friend. Given that this is reading from a device, you're probably dealing with physical quantities, but I thought it worth mentioning.根据您正在读取的读数类型(以及它们如何通信),您可能希望使用十进制而不是双精度。可能不是温度和气压(或任何其他“物理”值),但如果你正在处理金钱这样的人为价值,小数就是你的朋友。鉴于这是从设备读取,你可能正在处理物理量,但我认为值得一提。
-
For dealing with the collections of readings, you really want to look into LINQ. It's lovely :)
为了处理读数集合,你真的想看看LINQ。真可爱:)
#2
This type is small enough that, assuming it is immutable, you may like to consider using a struct. This will reduce the load on the garbage collected heap.
这种类型足够小,假设它是不可变的,您可能会考虑使用结构。这将减少垃圾收集堆上的负载。
What exactly are you trying to optimise? Memory footprint? Read performance? How will you be reading the data?
您究竟想要优化什么?内存占用?阅读表现?你将如何阅读数据?
#3
If you plan to put many X
object in an array, I suggest making it a struct
since it holds just a few "plain old data" items (no objects). Other than that, it seems like a straightforward way to work with the data.
如果你计划将许多X对象放在一个数组中,我建议将它作为一个结构,因为它只包含一些“普通的旧数据”项(没有对象)。除此之外,它似乎是一种简单的数据处理方式。
#4
There is nothing particularly wrong with the way you are already doing it. (Although as others have suggested, you should be making your fields private and using public properties instead)
你已经这样做的方式没有什么特别的错误。 (尽管正如其他人所建议的那样,你应该将你的字段设为私有并改为使用公共属性)
Often the best way to decide upon what storage structure to use is to consider how you will be using the data.
通常,决定使用哪种存储结构的最佳方法是考虑如何使用数据。
For example, if you know you need to process the data in some kind of order, consider a SortedList instead. If you know you need direct access to the data from some kind of key or index, use a Dictionary.
例如,如果您知道需要以某种顺序处理数据,请考虑使用SortedList。如果您知道需要从某种键或索引直接访问数据,请使用词典。