Why creating struct with constructor more slower than direct assignment? In code below I get 10 second with custom constructor and 6 second without! Pure cycle take a 5 second. Custom constructor five (!sic) times slower than direct access.
Is there any hack or something to speed up a custom constructor?
class Program
{
public struct Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
for(int i =0; i < int.MaxValue; i++)
{
var a = new Point(i, i);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Restart();
for (int i = 0; i < int.MaxValue; i++)
{
var a = new Point();
a.x = i;
a.y = i;
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
Console.ReadLine();
}
}