Created
August 23, 2012 11:30
-
-
Save Measter/3435785 to your computer and use it in GitHub Desktop.
Stack Implementation Tests
This file contains 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; | |
using System.Diagnostics; | |
namespace StackProper | |
{ | |
class PStack<T> | |
{ | |
public class Node | |
{ | |
public Node Next; | |
public T Value; | |
} | |
private int m_count; | |
public Node Head; | |
public int Count | |
{ | |
get { return m_count; } | |
} | |
public void Push(T value) | |
{ | |
if ( value == null ) | |
throw new ArgumentException(); | |
Node n = new Node(); | |
n.Value = value; | |
if ( Head != null ) | |
n.Next = Head; | |
Head = n; | |
m_count++; | |
} | |
public T Pop() | |
{ | |
if ( m_count == 0 ) | |
return default( T ); | |
Node n = Head; | |
Head = n.Next; | |
m_count--; | |
return n.Value; | |
} | |
} | |
class Program | |
{ | |
static void Main( string[] args ) | |
{ | |
Stopwatch watch = new Stopwatch(); | |
watch.Start(); | |
for ( int i = 0; i < 10000; i++ ) | |
{ | |
DoStackTest(); | |
} | |
watch.Stop(); | |
Console.WriteLine("Time taken: {0}ms", watch.ElapsedMilliseconds); | |
Console.Read(); | |
} | |
private static void DoStackTest() | |
{ | |
PStack<int> stack = new PStack<int>(); | |
for ( int i = 0; i < 78000; i++ ) | |
{ | |
stack.Push( i ); | |
} | |
int j; | |
while ( stack.Count > 0 ) | |
{ | |
j = stack.Pop(); | |
} | |
} | |
} | |
} |
This file contains 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; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
namespace NetStack | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Stopwatch watch = new Stopwatch(); | |
watch.Start(); | |
for( int i = 0; i < 10000; i++ ) | |
{ | |
DoStackTest(); | |
} | |
watch.Stop(); | |
Console.WriteLine( "Time taken: {0}ms", watch.ElapsedMilliseconds ); | |
Console.Read(); | |
} | |
private static void DoStackTest() | |
{ | |
Stack<int> stack = new Stack<int>(); | |
for ( int i = 0; i < 78000; i++ ) | |
{ | |
stack.Push( i ); | |
} | |
int j; | |
while ( stack.Count > 0 ) | |
{ | |
j = stack.Pop(); | |
} | |
} | |
} | |
} |
This file contains 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; | |
using System.Collections; | |
using System.Diagnostics; | |
namespace NetStack | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Stopwatch watch = new Stopwatch(); | |
watch.Start(); | |
for( int i = 0; i < 10000; i++ ) | |
{ | |
DoStackTest(); | |
} | |
watch.Stop(); | |
Console.WriteLine( "Time taken: {0}ms", watch.ElapsedMilliseconds ); | |
Console.Read(); | |
} | |
private static void DoStackTest() | |
{ | |
Stack stack = new Stack(); | |
for ( int i = 0; i < 78000; i++ ) | |
{ | |
stack.Push( i ); | |
} | |
int j; | |
while ( stack.Count > 0 ) | |
{ | |
j = (int)stack.Pop(); | |
} | |
} | |
} | |
} |
This file contains 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; | |
using System.Diagnostics; | |
namespace PointerStack | |
{ | |
unsafe struct Node | |
{ | |
public Node* Next; | |
public void* Value; | |
} | |
unsafe struct PStack | |
{ | |
public int Count; | |
public Node* Head; | |
} | |
unsafe class Program | |
{ | |
static void Main( string[] args ) | |
{ | |
Stopwatch watch = new Stopwatch(); | |
watch.Start(); | |
for( int i = 0; i < 10000; i++ ) | |
{ | |
DoStackTest(); | |
} | |
watch.Stop(); | |
Console.WriteLine("Time taken: {0}ms", watch.ElapsedMilliseconds); | |
Console.Read(); | |
} | |
private static void DoStackTest() | |
{ | |
PStack* stack = stackalloc PStack[1]; | |
for ( int i = 0; i < 78000; i++ ) | |
{ | |
Node* n = stackalloc Node[1]; | |
int* a = stackalloc int[1]; | |
a[0] = i; | |
StackPush( stack, n, a ); | |
} | |
int* j; | |
while ( stack->Count > 0 ) | |
{ | |
j = (int*)StackPop( stack ); | |
} | |
} | |
private static void* StackPop( PStack* pStack ) | |
{ | |
if( pStack == null ) | |
throw new ArgumentException(); | |
if ( pStack->Count == 0 ) | |
return null; | |
Node* node = pStack->Head; | |
pStack->Head = node->Next; | |
pStack->Count--; | |
return node->Value; | |
} | |
private static void StackPush( PStack* pStack, Node* node, void* value ) | |
{ | |
if( pStack == null || node == null || value == null ) | |
throw new ArgumentException(); | |
node->Value = value; | |
if( pStack->Head != null ) | |
node->Next = pStack->Head; | |
pStack->Head = node; | |
pStack->Count++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment