Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace RunUO
{
public class LoadMul
{
public static List<short> m_ItemIDs;
const string sStatic = "statics0.mul";
public static void Main()
{
if( !File.Exists( sStatic ) )
{
Console.WriteLine( "File not exist!" );
Console.ReadKey();
return;
}
System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.AboveNormal;
Console.WriteLine( "\t\t\tTime (ms)" );
Console.Write( "Original function\t" );
Test( LoadMul_0 );
Console.Write( "Remove Seek\t\t" );
Test( LoadMul_1 );
Console.Write( "Remove Contains\t\t" );
Test( LoadMul_2 );
Console.Write( "Use BufferedStream\t" );
Test( LoadMul_3 );
Console.Write( "Remove BinaryReader\t" );
Test( LoadMul_4 );
Console.Write( "Move buffer allocation\t" );
Test( LoadMul_5 );
Console.ReadKey();
}
public delegate void Callback();
public static void Test( Callback callback )
{
System.Threading.Thread.Sleep( 25 );
m_ItemIDs = new List<short>();
Stopwatch time = new Stopwatch();
time.Start();
callback();
time.Stop();
m_ItemIDs = null;
Console.WriteLine( "{0}", time.ElapsedMilliseconds );
}
public static void LoadMul_0()
{
using( Stream pStatic = new FileStream( sStatic, FileMode.Open ) )
{
BinaryReader reader = new BinaryReader( pStatic );
long length = pStatic.Length / 7;
for( int i = 0; i < length; i++ )
{
short id = reader.ReadInt16();
if( !m_ItemIDs.Contains( id ) )
m_ItemIDs.Add( id );
pStatic.Seek( 5, SeekOrigin.Current );
}
}
}
public static void LoadMul_1()
{
using( Stream pStatic = new FileStream( sStatic, FileMode.Open ) )
{
BinaryReader reader = new BinaryReader( pStatic );
long length = pStatic.Length / 7;
for( int i = 0; i < length; i++ )
{
short id = reader.ReadInt16();
if( !m_ItemIDs.Contains( id ) )
m_ItemIDs.Add( id );
reader.ReadBytes( 5 );
}
}
}
public static void LoadMul_2()
{
using( Stream pStatic = new FileStream( sStatic, FileMode.Open ) )
{
BinaryReader reader = new BinaryReader( pStatic );
long length = pStatic.Length / 7;
bool[] buff = new bool[0x4000];
for( int i = 0; i < length; i++ )
{
short id = reader.ReadInt16();
buff[id] = true;
reader.ReadBytes( 5 );
}
for( short i = 0; i < buff.Length; i++ )
if( buff[i] )
m_ItemIDs.Add( i );
}
}
public static void LoadMul_3()
{
using( Stream pStatic = new BufferedStream( new FileStream( sStatic, FileMode.Open ), 64 * 1024 ) )
{
BinaryReader reader = new BinaryReader( pStatic );
long length = pStatic.Length / 7;
bool[] buff = new bool[0x4000];
for( int i = 0; i < length; i++ )
{
short id = reader.ReadInt16();
buff[id] = true;
reader.ReadBytes( 5 );
}
for( short id = 0; id < buff.Length; id++ )
if( buff[id] )
m_ItemIDs.Add( id );
}
}
public static void LoadMul_4()
{
using( Stream pStatic = new BufferedStream( new FileStream( sStatic, FileMode.Open ), 64 * 1024 ) )
{
long length = pStatic.Length / 7;
bool[] buff = new bool[0x4000];
for( int i = 0; i < length; i++ )
{
byte[] buffer = new byte[7];
pStatic.Read( buffer, 0, 7 );
int id = (buffer[1] << 8) + buffer[0];
buff[id] = true;
}
for( int id = 0; id < buff.Length; id++ )
if( buff[id] )
m_ItemIDs.Add( (short)id );
}
}
public static void LoadMul_5()
{
using( Stream pStatic = new BufferedStream( new FileStream( sStatic, FileMode.Open ), 64 * 1024 ) )
{
long length = pStatic.Length / 7;
bool[] buff = new bool[0x4000];
byte[] buffer = new byte[7];
for( int i = 0; i < length; i++ )
{
pStatic.Read( buffer, 0, 7 );
int id = (buffer[1] << 8) + buffer[0];
buff[id] = true;
}
for( int id = 0; id < buff.Length; id++ )
if( buff[id] )
m_ItemIDs.Add( (short)id );
}
}
}
}
Intel Pentium 4 - Nortwood 3 GHz, RAM 3 GB PC3200
HD 300 GB MaxLine III 7200 rpm, SATA, 16 MB cache
Hyperthreading: Enabled
Code:
Time (ms)
Original function 26866
Remove Seek 11357
Remove Contains 529
Use BufferedStream 475
Remove BinaryReader 281
Move buffer allocation 205
Hyperthreading: Disabled
Code:
Time (ms)
Original function 26289
Remove Seek 11240
Remove Contains 523
Use BufferedStream 484
Remove BinaryReader 293
Move buffer allocation 189