Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C#] Safe Array Manipulation vs. Unsafe Array Manipulation

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sat Nov 24, 2007 1:17 am    Post subject: [C#] Safe Array Manipulation vs. Unsafe Array Manipulation Reply with quote

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites