views:

189

answers:

6

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?

A: 

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

Hogan
This can waste space though.
Hamish Grubijan
A: 

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...

Randolpho
+1  A: 

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.

Yuriy Faktorovich
+1 i love this answer!
used2could
A: 

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.

EKS
A: 

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 ...

Hamish Grubijan
Won't XML use unnecessary space? It's not much information I want to store but I've today pressed over 25k keys and 5k mouse clicks and that would make a large footprint, think it's the right term, if I use XML?
Zolomon
Ok, use simple ASCII format then: [YYMMDDhhmmss FF][YYMMDDhhmmss 123,254 R][YYMMDDhhmmss 123,254 L][YYMMDDhhmmss 124,256][YYMMDDhhmmss 45] - here FF, 45 are hex representations of pressed keys (Assuming ASCII). Still, I would recommend using classes. Writing out should be easy. Parsing - not that hard either.
Hamish Grubijan
+1  A: 

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

Vivek Bernard