samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Nov 24, 2007 1:17 am Post subject: [C#] Safe Array Manipulation vs. Unsafe Array Manipulation |
|
|
This is something I've been meaning to do for a while--compare "safe" code against "unsafe" code.
So I wrote the following Console App:
| Code: |
using System;
namespace SpeedTester
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[50000]; //Declare our array with a massive number of elements
//Initialize
for (int i = 0; i < arr.Length; i++)
{
arr[i] = i;
}
//Test 1
Console.WriteLine("##################");
Console.WriteLine("## TEST 1 ##");
Console.WriteLine("##################");
DateTime test1begin = DateTime.Now;
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(i.ToString());
}
DateTime test1end = DateTime.Now;
Console.WriteLine("Begin : " + test1begin.ToString());
Console.WriteLine("End : " + test1end.ToString());
TimeSpan test1elapased = test1end.Subtract(test1begin);
Console.WriteLine("Elapsed time : " + test1elapased.ToString());
//Test 2
Console.WriteLine("##################");
Console.WriteLine("## TEST 2 ##");
Console.WriteLine("##################");
DateTime test2begin = DateTime.Now;
Console.WriteLine("Begin : " + DateTime.Now.ToString());
unsafe
{
fixed (int* unsafearr = &arr[0])
{
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(unsafearr[i]);
}
}
}
DateTime test2end = DateTime.Now;
Console.WriteLine("End : " + DateTime.Now.ToString());
TimeSpan test2elapased = test2end.Subtract(test2begin);
Console.WriteLine("Elapsed time : " + test2elapased.ToString());
Console.WriteLine("Test comparisons...");
Console.WriteLine("Elapsed time for test 1 (\"safe\" code) : " + test1elapased.ToString());
Console.WriteLine("Elapsed time for test 2 (\"unsafe\" code) : " + test2elapased.ToString());
Console.ReadLine();
}
}
}
|
The results were as follows:
| Code: |
Elapsed time for test 1 ("safe" code) : 00:00:16.8125000
Elapsed time for test 2 ("unsafe" code) : 00:00:15.7968750
|
Now, mind you, this was with VS attatched to it, in Debug mode, so optimizations can be made, but I wanted to show this to everyone (including myself) at its worst.
~~
So what's my conclusion?
You shave about a second per 50000 elements, but the code looks kinda nasty; seriously compare:
| Code: |
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(i.ToString());
}
|
| Code: |
unsafe
{
fixed (int* unsafearr = &arr[0])
{
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(unsafearr[i]);
}
}
}
|
You're also increasing the program's size, and all the extra lines, just for a second of time extra. Imagine more complex things.
Is it really worth it?
Edit:
I'm gonna test it out with a bit more complicated things in unsafe code next. =)
Edit:
My bad! I messed up in the testing. Here is the new test 1:
| Code: |
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i].ToString());
}
|
And the new results:
| Code: |
Elapsed time for test 1 ("safe" code) : 00:00:17.0312500
Elapsed time for test 2 ("unsafe" code) : 00:00:14.3906250
|
So near 3 seconds in 50k elements. My previous answer still pretty much stands.
|
|