Systematic Gaming

Code: CachePressure C#

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

namespace CachePressureCS
{
unsafe struct Data
{
public int key;
public fixed char data[128];
}

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 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 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 += SortTestC( 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 );
}

Console.WriteLine();
}
}
}
}

1 Comment »

  1. you should have used System.Diagnostics.Stopwatch instead of P/invokes.

    Comment by pinvoke — January 3, 2009 @ 10:56 pm


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: