Skip to content

Instantly share code, notes, and snippets.

@cthom06
Last active December 30, 2015 05:09
Show Gist options
  • Save cthom06/7781155 to your computer and use it in GitHub Desktop.
Save cthom06/7781155 to your computer and use it in GitHub Desktop.
array example
using System;
namespace Foo
{
class Program
{
public static void Main(string[] args)
{
const int TestSize = 1000000;
// guess which is faster and by approx. how much
// then guess why
var firstCase = new Foo[TestSize];
var secondCase = new Bar[TestSize];
for (int i = 0; i < TestSize; i++)
secondCase[i].Value = firstCase[i] = new Foo();
// Is this faster?
for (int i = 0; i < TestSize; i += 50)
{
var tmp = new Foo[50];
for (int j = 0; j < 50; j++)
{
tmp[j] = firstCase[i + j];
tmp[j].X += 7;
}
}
// Or this?
for (int i = 0; i < TestSize; i += 50)
{
var tmp = new Bar[50];
for (int j = 0; j < 50; j++)
{
tmp[j] = secondCase[i + j];
tmp[j].Value.X += 11;
}
}
}
}
class Foo
{
public int X, Y;
}
struct Bar
{
public Foo Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment