Last active
December 30, 2015 05:09
-
-
Save cthom06/7781155 to your computer and use it in GitHub Desktop.
array example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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