我应该如何保存我的数据?

发布于 2024-08-18 04:24:08 字数 494 浏览 4 评论 0原文

我有两个这样的结构:

public struct KeyLog
{
    Keys key;
    DateTime time;
}

public struct MouseLog
{
    MouseEvents mouse;
    Point coordinates;
    DateTime time;
}

对于每次键盘按键和鼠标按钮单击,我希望保存此数据,但我不知道哪种方式存储/处理它最有效?是否有更好的方法来处理数据 - 我不确定使用两个结构是否比将两者合并为一个更好?

编辑:我正在做一个键盘&鼠标统计应用程序将存储按键次数和按键次数鼠标单击以及按下的按钮、计算机的位置和时间,我希望每次按下按钮时都保存这些数据。不一定每次都写入磁盘,但至少将其存储在内存中,直到我想将其保存到磁盘。

编辑:我认为,如果我将两个结构分开,当我存储它们时,我不会创建太多死数据,然后如果我将它们分开,我可以轻松搜索/排序。想法?

I have two structs like so:

public struct KeyLog
{
    Keys key;
    DateTime time;
}

public struct MouseLog
{
    MouseEvents mouse;
    Point coordinates;
    DateTime time;
}

For every keyboard key press and mousebutton click I wish to save this data but I do not know which way would be the most efficient to store/handle it in? Are there better ways to handle the data - I'm not sure if using two structs is better than merging both into one?

Edit: I'm doing a keyboard & mouse statistic application that'll store the amount of key press & mouse clicks as well as what button was pressed, where and when for my computer and I would want to save this data every time a button is pressed. Not necessarily write to disk every time but at least store it in memory till I want to save it to disk.

Edit: I thought that if I keep the two structs separate I won't create too much dead data when I store them, and then I can easily search/sort if I keep them separate. Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

遥远的她 2024-08-25 04:24:08

BinaryFormatter 会给出你最小的压缩。此外,如果将它们更改为类,则可以拥有一个带有 DateTime 时间 字段的基类。

A BinaryFormatter would give you the smallest compression. Also if you change them to classes, you could have a base class with a DateTime time field.

仙女山的月亮 2024-08-25 04:24:08

合并它们将使它们易于编程。

但是,当您更加意识到节省磁盘空间时,

您可以使用 MouseLog 和 Keylog 的通用集合,

例如列表
列出内存表示

AS 将其写入磁盘,您可以将它们放入一个类中并将它们保存为一个大对象,

例如

  [serializable]
    class BigObject 
    {

    List<Mouselog> MouseLogLst;


    List<KeyLog>  KeyLogLst ;

    }

并使用 二进制格式化程序
存储它们

Merging them will make them easy to program with..

BUt as you are more conscious about saving disk space

You could use a generic collection of MouseLog and Keylog

eg List
List for in memory representation

AS for writing it to a disk you could put them in a class and save them as one Big object

like

  [serializable]
    class BigObject 
    {

    List<Mouselog> MouseLogLst;


    List<KeyLog>  KeyLogLst ;

    }

and use Binary Formmater
to store them

甜警司 2024-08-25 04:24:08

我经常将其按顺序保存到具有非常简单的数据访问层的二进制文件中。您可以在这里使用几种方法,但我最喜欢这个,它相当简单而且速度很快。您甚至可以非常轻松地为文件中的条目创建索引表。

class Foo {
  private int _someInteger;
  private string _someString;

  public virtual void Serialize(BinaryWriter writer) {
    writer.Write(2); // A simple versioning, start at 0, then increment.
    writer.Write(_someString);
    writer.Write(_someInteger);
  }

  public virtual void Deserialize(BinaryReader reader) {
    int version = reader.ReadInt32();
    switch (version) {
      case 2: // the string was only added in version 2 of the class serialization.
        _someString = reader.ReadString(); 
        goto case 1;
      case 1: // continue at case 1.
        _someInteger = reader.ReadInt32();
        break; // break here, because version 0 was very different.
      case 0: // in version 0 of the class there was actually a double, but it was always an integral number, so this basically is a conversion case.
        _someInteger = (int)reader.ReadDouble();
        break;
    }
  }
}

请注意,从文件中读取它们的顺序与写入它们的顺序相同。

然后,您可以将流中的读取器和写入器传递给它,例如,

Foo fooInstance = new Foo();
FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fileStream);

fooInstance.Deserialize(reader);

它非常简单,您不必使用愚蠢的 XML 序列化器类的设置访问器来公开所有字段和属性,这总是给您带来问题。

I often save it sequentially into a binary file with a very simple data access layer. There are a few approaches you can use here but I like this one the most, it's rather simple and it's fast. You can even create an index table to entries in the file very easily and much more.

class Foo {
  private int _someInteger;
  private string _someString;

  public virtual void Serialize(BinaryWriter writer) {
    writer.Write(2); // A simple versioning, start at 0, then increment.
    writer.Write(_someString);
    writer.Write(_someInteger);
  }

  public virtual void Deserialize(BinaryReader reader) {
    int version = reader.ReadInt32();
    switch (version) {
      case 2: // the string was only added in version 2 of the class serialization.
        _someString = reader.ReadString(); 
        goto case 1;
      case 1: // continue at case 1.
        _someInteger = reader.ReadInt32();
        break; // break here, because version 0 was very different.
      case 0: // in version 0 of the class there was actually a double, but it was always an integral number, so this basically is a conversion case.
        _someInteger = (int)reader.ReadDouble();
        break;
    }
  }
}

Just take care you read them from the file in the same order as you write them.

You could then just pass it the readers and writers from a stream, e.g.

Foo fooInstance = new Foo();
FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fileStream);

fooInstance.Deserialize(reader);

It's so simple and you don't have to expose all fields and properties with set-accessors for the stupid XML serializer classes that gives you problems all the time.

病女 2024-08-25 04:24:08

除非有充分的理由将它们分开,否则您应该合并它们。我相信 KISS 在这里适用。

Unless there is a good reason to keep them separate you should merge them. KISS applies here I believe.

2024-08-25 04:24:08

呃......你打算把它存储在哪里?如果您只是想要一个可以参考的内存中日志,那么 List 应该没问题。

如果您想将其流式传输到磁盘,则需要对其进行序列化。就总磁盘空间而言,二进制序列化是最高效的,但 XML 序列化是“人类可读的”。

然后是数据库存储...

er.... where are you planning to store it? If you're just wanting an in-memory log that you can refer to, a List<X> should be fine.

If you want to stream it to disk, you'll want to serialize it. Binary serialization is the most efficient in terms of total disk space, but XML serialization is "human readable".

Then there's database storage...

望笑 2024-08-25 04:24:08

您是否需要担心 Unicode 还是可以是 ASCII?我想说 XML 是你的朋友。不要合并这两个类,而是创建两个类,它们都是抽象 EventLog 的子级。 EventLog 类必须强制您使用 toString() 方法,也许还有其他一些方法。然后有一个事件日志列表,其中每个事件日志都是一个按键日志或一个鼠标日志。我想知道你是否在重新发明轮子......

Do you have to worry about Unicode or can this be ASCII? I would say XML is your friend. Do not merge the two, but make both classes, which are both Children of an abstract EventLog. EventLog class must force a toString() method on you, maybe a few others. Then have a list of EventLogs, where each EventLog is a KeyLog or a MouseLog. I wonder if you are reinventing the wheel ...

盛夏已如深秋| 2024-08-25 04:24:08

我会将其存储在一个结构中,因为它很可能会节省您作为程序员的时间。处理起来会更简单,尤其是稍后保存/加载文件时。使用您所描述的结构几乎没有什么收获。

如果这成为性能问题,请分析代码以查看瓶颈所在,然后修复它们。

I would store it in one struct, beacuse it will most likely save you time as a programmer. It will be simpler to deal with, especially when saving/loading the file later on. There is little to be gained from using structs which you have described.

And if that should become a performance problem, profile the code see where any bottlenecks are and then fix them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文