Initialization and access performance tests

I was wondering many times whether to use structure or class, how I should initialize structure, and ofcourse about array vs list accessibility issue. There is a lot of tests on the web, however I decided to manage some sort of them by myself and put the results here.

1. Structure vs Class – initialization issues

As we know or not, structures are pretended to be a value-type whilst class reference-type. I should mention that it is good to use structures to be immutable and have value-type fields. Structures are being passed by value, not by reference like class objects. So if you pass structure object to function you pass its copy. Ok, but this is about performance of initialization structures and classes with new or without new, so..

Here we have simple structure and class representing Point in 2D.

public struct Point2D
{
    public int x, y;
    public Point2D(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

public class Point2DCl
{
    public int x, y;
    public Point2DCl(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

void test()
{
    sw.Start();
    for (int i = 0; i < 100000000; i++)
    {
        Point2DCl p1 = new Point2DCl(10, 12);
    }
    sw.Stop();
    Console.WriteLine("My Point class: " + sw.ElapsedTicks);
    sw.Reset();

    sw.Start();
    for (int i = 0; i < 100000000; i++)
    {
        Point p1 = new Point(10, 12);
    }
    sw.Stop();
    Console.WriteLine(".NET Point struct: " + sw.ElapsedTicks);
    sw.Reset();

    sw.Start();
    for (int i = 0; i < 100000000; i++)
    {
        Point2D p2 = new Point2D(10, 12);
    }
    sw.Stop();
    Console.WriteLine("My Point struct initialized with new: "
                                            + sw.ElapsedTicks);
    sw.Reset();

    sw.Start();
    for (int i = 0; i < 100000000; i++)
    {
        Point2D p3;
        p3.x = 10;
        p3.y = 12;
    }
    sw.Stop();
    Console.WriteLine("My Point struct initialized w/o new: "
                                            + sw.ElapsedTicks);
}

 
Results in elapsed ticks:
My Point class: 812775
.NET Point struct: 72452
My Point struct initialized with new: 72527
My Point struct initialized w/o new: 72493

As we see, there is no really difference between any kind of structure initialization but there is some performance lost with class object initialization. In conclusion, it is better to use structures than classes if it is needed or meaningful as with the 2D Point case.

2. Array, List, Dictionary – access performance

I think there is nothing left to say, because results are rather predictable, just see..

int[] tab = { 1, 2, 3, 4, 5 };

List<int> list = tab.ToList<int>();

Dictionary<int, int> dict = new Dictionary<int, int>();
dict.Add(0, 1);
dict.Add(1, 2);
dict.Add(2, 3);
dict.Add(3, 4);
dict.Add(4, 5);

void test()
{
    int a = 0;
    sw.Start();
    for (int i = 0; i < 10000000; i++)
    {
        for (int n = 0; n < 5; n++)
        {
            a = tab[n];
        }
    }
    sw.Stop();
    Console.WriteLine("Access array: " + sw.ElapsedTicks);
    sw.Reset();

    int b = 0;
    sw.Start();
    for (int i = 0; i < 10000000; i++)
    {
        for (int n = 0; n < 5; n++)
        {
            b = list[n];
        }
    }
    sw.Stop();
    Console.WriteLine("Access list: " + sw.ElapsedTicks);
    sw.Reset();

    int c = 0;
    sw.Start();
    for (int i = 0; i < 10000000; i++)
    {
        for (int n = 0; n < 5; n++)
        {
            c = dict[n];
        }
    }
    sw.Stop();
    Console.WriteLine("Access dictionary: " + sw.ElapsedTicks);
    sw.Reset();
}

Results in elapsed ticks:
Access array: 80042
Access list: 187826
Access dictionary: 1636323

I suppose no comment is needed, it is just for visualize the differences in time access and possible performance losts if use hastily. It is good habit to set list capacity if it is already known.

This entry was posted in C# and tagged , . Bookmark the permalink.

1 Response to Initialization and access performance tests

  1. cfaniak says:

    You should consider testing bigger size of data structures. In this case maybe you got lucky and all elements of list got in the same cache page in result it become faster than it really is :P

Leave a reply to cfaniak Cancel reply