开发者

Estimate the memory usage in .NET (VS)

开发者 https://www.devze.com 2022-12-14 05:02 出处:网络
I read an article about the performance of the string methods \"a\" + b versus string.Format(\"a{1}\", b) and decided to make a test.

I read an article about the performance of the string methods "a" + b versus string.Format("a{1}", b) and decided to make a test.

There is my code in a WinForms application(VS 2005, but interested in both 2005/2008).

    private void button1_Click(object sender, EventArgs e)
    {
        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Star开发者_开发技巧t();
        for (int i = 0; i < 10000; i++)
        {
            Console.Write("1" + 2 + "3" + 4 + "5");
        }
        stopwatch.Stop();
        Console.WriteLine("**********");
        Console.WriteLine("'A'+'B'+'C'... 10 000 elapsed: {0}", 
            stopwatch.Elapsed.ToString());
        Console.WriteLine("**********");
        stopwatch.Reset();

        stopwatch.Start();
        for (int i = 0; i < 10000; i++)
        {
            Console.Write(string.Format("1{0}3{1}5", 2, 4));
        }
        stopwatch.Stop();
        Console.WriteLine("**********");
        Console.WriteLine("string.Format((A)B(C)...) 10 000 elapsed: {0}", 
            stopwatch.Elapsed.ToString());
        Console.WriteLine("**********");

    }

on my machine the output is the following:


12345123451234512345123451234512345123451234512345123451...

'A'+'B'+'C'... 10 000 elapsed: 00:00:0 9.4217880


12345123451234512345123451234512345123451234512345123451...

string.Format((A)B(C)...) 10 000 elapsed: 00:00:0 9.8507060


So, the time is almost the same.

Now, I wonder how much memory I used. Is there a way to compare the memory usage and the GarbageCollector work?

I downloaded a trial .NET Memory Profiler... but maybe there is a more simple(code like above) way to detect the spent memory on both iterations?

Thanks.


GC.GetTotalMemory might be accurate enough:

http://msdn.microsoft.com/en-us/library/system.gc.gettotalmemory.aspx

Otherwise, you could use the CLR GC Heap performance counters via System.Diagnostics.PerformanceCounter.


My recommendations would be...

I would take out the console.writeline in the loops because Console.WriteLine introduces its own observer effect into the time elapsed. Just assign it to variable or similar.

You also may not be giving String.Format a fair break... you have no idea whether the GC has run between your two loops. GC may be kicking in while you're inside the second loop, skewing your time. Or it may not be kicking in at all. I would be sure of that or run the test in the opposite order as well to be sure.

Strings are a special case in general. Many of the issues with strings in .NET actually come from how windows deals with strings. Best way I can put it is that strings aren't as cleanly GC'ed as other objects are, so anything I can do to avoid creating more strings than I need to I take. Of course, we deal with lots of strings because that's what we do, so I'm only talking about building extra strings.

Example: It seems cool to build up SQL commands from a template with String.Format("select {0} from {1} where {2}",selectStmt, fromStmt, whereStmt), but it's more efficient to just keep the selects your going to need as consts or similar, especially if you are going to fire them a lot and be building that same string over and over again. It's also less susceptible to SQL injection (not digressing into that at this point). That's just by way of example, I'm not claiming you shouldn't use an ORM etc.

Check out http://blog.briandicroce.com/2008/02/04/stringbuilder-vs-string-performance-in-net/

0

精彩评论

暂无评论...
验证码 换一张
取 消