views:

408

answers:

4

I have a filename that has the following format:

timestamp-username-1

This file is constantly etting written to, but before it gets too large I want to create a new file.

timestamp-username-2

How can I acheive this with using least amount of memory (ie, no or little variables)

here is my version:

        private void Split() {
            char[] strArr = FlowArgs.Filename.ToCharArray();
            int num;
            //get the last number
            if(Int32.TryParse(strArr[strArr.Length - 1].ToString(), out num)) {
                num += 1;
            }
            //replace the old number with the new number
            char.TryParse(num.ToString(), out strArr[strArr.Length - 1]);
            FlowArgs.Filename = strArr.ToString();

        }

Edit: I have added a "version" property (int) in the FlowArgs class. However my new problem is that how can I append this at the end of thefilename

+2  A: 

"least amount of memory" is NOT equals to "no or little variables"

Local variables only takes little memory itself. But object creation in heap takes a lot more, and they require GC to do cleanup.

In your example, your ToCharArray() and ToString() have created 4 object (indirect created object not included).

Dennis Cheung
What would you recommend then? FlowArgs is a class that inherits from EventArgs.. I guess I could just add another property called "version" and increment that?
masfenix
Please see my edit.
masfenix
+5  A: 

I think you should just store the counter in an int. I understand that you want to save memory space but to be honest, an extra int is really in the "acceptable" category. I mean, the Int32 parser is probably wasting way much more memory. Don't forget that on x86, the memory space is spitted to 4096 byte pages so there is much more memory wasted than these 4 bytes.

EDIT: You probably want to have a method like GetNextFileName() in your class that generates you the next filename (being able to refactor your code into small bits is important, much more important than saving memory space):

private int nextFileNumber = 0;

private string GetNextFileName(string userName)
{
    return String.Format("{0}-{1}-{2}", DateTime.Now, userName,
       nextFileNumber++);
}
DrJokepu
+1  A: 

your string variable is already character array:

    int num=0;
    //get the last number
    if (Int32.TryParse(FolwArgs.Filename[FolwArgs.Filename.Length-1].ToString(), out num))
        num++;
    //replace the old number with the new number
    char.TryParse(num.ToString(), out FolwArgs.Filename[FolwArgs.Filename.Length-1]]);
Igor Zelaya
A: 

Instead of using a running counter, consider using datetime of creation as the changing part of your filename. This way, you don't have to store and retrieve the previous value. Using the ToBinary() method, you can get a numeric representation of the time. Of course, any time format that is acceptable in a filename can be used - see custom date and time format strings.

gimel