C# Structural List Loop Processing Time Question

Asked 2 years ago, Updated 2 years ago, 125 views

Hello, I'm testing the execution time of the structure list, but I found something interesting when I didn't know.
TransformSystem is the class that operates structure Vector3.The method MoveObject adds the items in the list one by one.

The main method measures the execution time.Loop count should be from 50,000 to 400,000.
The results are shown below.
There is nothing wrong with 50,000 results, but all results after 100,000 will be the same.

My question is:
Is there a problem with my code?If there is no problem, why does this happen?

public class TransformSystem
{
    private readonly list <Vector3>vectorArray;

    public TransformSystem (int capacity)
    {
        vectorArray=new List<Vector3>(capacity);
    }

    public void MoveObject (ref float x, ref floaty, ref float z)
    {
        for (inti=0;i<vectorArray.Count;i++)
        {
            varvector3 = vectorArray[i];
            vector 3.x+=x;
            vector3.y+=y;
            vector3.z+=z;
            vectorArray[i] = vector3;
        }
    }

    public void MoveObject (ref Vector 3 direction)
    {
        for (inti=0;i<vectorArray.Count;i++)
        {
            varvector=vectorArray[i];

            vector.x + = direction.x;
            vector.x+=direction.y;
            vector.x+=direction.z;
            vectorArray[i] = vector;
        }
    }
}

internal class program
{
    public static void Main (string[]args)
    {
        int count = 50000;
        while (count <500000)
        {
            Console.WriteLine($"===========Count:{count}===========");
            Execute(count);
            count*=2;
        }
    }

    private static void execute (int count)
    {
        Vector3 direction = new Vector3 {x=1,y=1,z=1};

        Stopwatch sw = new Stopwatch();

        sw.Start();
        TransformSystem system=new TransformSystem(count);
        sw.Stop();
        Console.WriteLine("struct initialization finished.Elapped:"+sw.Elapped);
        sw.Reset();

        sw.Start();
        system.MoveObject(ref direction);
        sw.Stop();
        Console.WriteLine("ref structure moved finished.Elapped:"+sw.Elapped);

        sw.Start();
        system.MoveObject(ref direction.x,ref direction.y,ref direction.z);
        sw.Stop();
        Console.WriteLine("ref structure value moved finished.Elapped:"+sw.Elapped);
    }
}

===========Count: 50,000 ===========
structure initialization finished.Elapped:00:00.0013711
ref structure move finished.Elapped:00:00.0002028
ref structure value move finished.Elapped:00:00.0004169
===========Count: 100000 ===========
structure initialization finished.Elapsed:00:00:00.0000118
ref structure move finished.Elapped:00:00.000000001
ref structure value move finished.Elapped:00:00.0000002
===========Count: 200000 ===========
struct initialization finished.Elapsed:00:00:00.0000061
ref structure move finished.Elapped:00:00.000000001
ref structure value move finished.Elapped:00:00.0000002
===========Count: 400000 ===========
structure initialization finished.Elapped:00:00.0000062
ref structure move finished.Elapped:00:00.000000001
ref structure value move finished.Elapped:00:00.0000002

c# compiler

2022-09-30 15:36

1 Answers

There is no loop in MoveObject().vectorArray is always zero because you do not add elements to vectorArray.Therefore, no matter what the value of the count is, the execution time is unlikely to change.

Count: 50,000 is only slow because it's the first code execution.


2022-09-30 15:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.