Systematic Gaming

Code: Cache Pressure C# 2008

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace CachePressureCS
{
struct Data
{
public int key;

public int a0, a1, a2, a3, a4, a5, a6, a7;
public int b0, b1, b2, b3, b4, b5, b6, b7;
public int c0, c1, c2, c3, c4, c5, c6, c7;
public int d0, d1, d2, d3, d4, d5, d6, d7;
}

class DataIndex
{
public DataIndex(int k, int i)
{
key = k;
index = i;
}
public int key;
public int index;
}

class DataComparer : IComparer
{
int IComparer.Compare( Object x, Object y )
{
return ((Data)x).key – ((Data)y).key;
}
}

class DataComparerT : IComparer<Data>
{
public int Compare(Data x, Data y)
{
return x.key – y.key;
}
}

class DataIndexComparerT : IComparer<DataIndex>
{
public int Compare(DataIndex x, DataIndex y)
{
return x.key – y.key;
}
}

class Timer
{
[DllImport( “Kernel32.dll” )]
private static extern bool QueryPerformanceCounter( out long counter );

[DllImport( “Kernel32.dll” )]
private static extern bool QueryPerformanceFrequency( out long frequency );

public Timer()
{
mStart = mEnd = 0;
QueryPerformanceFrequency( out mFrequency );
}

public void Start()
{
QueryPerformanceCounter( out mStart );
}

public void End()
{
QueryPerformanceCounter( out mEnd );
}

public double Time
{
get
{
return 1000.0 * (double)( mEnd – mStart ) / (double)mFrequency;
}
}

long mFrequency;
long mStart;
long mEnd;
}

class Program
{
private static int CompareData( Data x, Data y )
{
return x.key – y.key;
}

static double SortTest(int size)
{
Random rng = new Random(0);
Timer time = new Timer();
Data[] data = new Data[size];

for ( int i = 0; i < data.Length; i++ )
{
data[i].key = rng.Next();
}

time.Start();
Array.Sort( data, new DataComparer() );
time.End();

return time.Time;
}

static double SortTestT( int size )
{
Random rng = new Random( 0 );
Timer time = new Timer();
Data[] data = new Data[size];

for ( int i = 0; i < data.Length; i++ )
{
data[i].key = rng.Next();
}

time.Start();
Array.Sort<Data>( data, new DataComparerT() );
time.End();

return time.Time;
}

static double SortTestTC( int size )
{
Random rng = new Random( 0 );
Timer time = new Timer();
Data[] data = new Data[size];

for ( int i = 0; i < data.Length; i++ )
{
data[i].key = rng.Next();
}

time.Start();
Array.Sort<Data>( data, CompareData );
time.End();

return time.Time;
}

static double SortTestC( int size )
{
Random rng = new Random( 0 );
Timer time = new Timer();
Data[] data = new Data[size];

for ( int i = 0; i < data.Length; i++ )
{
data[i].key = rng.Next();
}

time.Start();
Array.Sort( data, CompareData );
time.End();

return time.Time;
}

static double SortTestIndirect( int size )
{
Random rng = new Random( 0 );
Timer time = new Timer();
Data[] data = new Data[size];
int[] indirect = new int[size];

for ( int i = 0; i < data.Length; i++ )
{
data[i].key = rng.Next();
indirect[i] = data[i].key;
}

time.Start();
Array.Sort<int, Data>( indirect, data );
time.End();

return time.Time;
}

static double SortTestIndirect2(int size)
{
Random rng = new Random(0);
Timer time = new Timer();
Data[] data = new Data[size];
DataIndex[] indirect = new DataIndex[size];
Data[] dataOut = new Data[size];

for (int i = 0; i < data.Length; i++)
{
data[i].key = rng.Next();
indirect[i] = new DataIndex(data[i].key, i);
}

time.Start();
Array.Sort<DataIndex>(indirect, new DataIndexComparerT());

for (int i = 0; i < data.Length; i++)
{
int index = indirect[i].index;
dataOut[i] = data[index];
}
time.End();

return time.Time;
}

static void Main( string[] args )
{
for ( int i = 0; i < 10; i++ )
{
int size = 1024 << i;

Console.Write( “{0},”, size );
{
double time = 0;
for ( int j = 0; j < 10; j++ )
{
time += SortTest( size );
}
time /= 10.0;
Console.Write( “{0},”, time );
}

{
double time = 0;
for ( int j = 0; j < 10; j++ )
{
time += SortTestT( size );
}
time /= 10.0;
Console.Write( “{0},”, time );
}

{
double time = 0;
for ( int j = 0; j < 10; j++ )
{
time += SortTestTC( size );
}
time /= 10.0;
Console.Write( “{0},”, time );
}

{
double time = 0;
for ( int j = 0; j < 10; j++ )
{
time += SortTestIndirect( size );
}
time /= 10.0;
Console.Write( “{0},”, time );
}

{
double time = 0;
for (int j = 0; j < 10; j++)
{
time += SortTestIndirect2(size);
}
time /= 10.0;
Console.Write(“{0},”, time);
}

Console.WriteLine();
}
}
}
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: