using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Bla
{
class Program
{
static void Main(string[] args)
{
List<object> stuff = new List<object>();
const int iterNum = 500;
const int modNum = 50000;
for (int z = 0; z < 5; z++)
{
TestVertex(iterNum, modNum);
TestVertexImmu(iterNum, modNum);
}
Console.ReadKey(true);
}
static void TestVertex(int iterNum, int modNum)
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < iterNum; i++)
{
Vertex v = new Vertex(new Vector2f(iterNum, iterNum), Color.Red);
for (int k = 0; k < modNum; k++)
v.Position = new Vector2f(modNum, modNum);
}
Console.WriteLine("Vertex: {0} x {1} modified: {2}", iterNum, modNum, watch.Elapsed.TotalMilliseconds);
}
static void TestVertexImmu(int iterNum, int modNum)
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < iterNum; i++)
{
VertexImmu w = new VertexImmu(new Vector2f(iterNum, iterNum), Color.Red);
for (int k = 0; k < modNum; k++)
w = new VertexImmu(new Vector2f(modNum, modNum), Color.Red);
}
Console.WriteLine("VertexImmu: {0} x {1} modified: {2}", iterNum, modNum, watch.Elapsed.TotalMilliseconds);
}
static void AddMemPressure(List<object> stuff)
{
stuff.Clear();
GC.Collect();
for (int i = 0; i < 500000; i++)
stuff.Add(new object());
}
/// <summary>
/// Define a point with color and texture coordinates
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Vertex
{
/// <summary>
/// Construct the vertex from its position
/// The vertex color is white and texture coordinates are (0, 0).
/// </summary>
/// <param name="position">Vertex position</param>
public Vertex(Vector2f position) :
this(position, Color.White, new Vector2f(0, 0))
{
}
/// <summary>
/// Construct the vertex from its position and color
/// The texture coordinates are (0, 0).
/// </summary>
/// <param name="position">Vertex position</param>
/// <param name="color">Vertex color</param>
public Vertex(Vector2f position, Color color) :
this(position, color, new Vector2f(0, 0))
{
}
/// <summary>
/// Construct the vertex from its position and texture coordinates
/// The vertex color is white.
/// </summary>
/// <param name="position">Vertex position</param>
/// <param name="texCoords">Vertex texture coordinates</param>
public Vertex(Vector2f position, Vector2f texCoords) :
this(position, Color.White, texCoords)
{
}
/// <summary>
/// Construct the vertex from its position, color and texture coordinates
/// </summary>
/// <param name="position">Vertex position</param>
/// <param name="color">Vertex color</param>
/// <param name="texCoords">Vertex texture coordinates</param>
public Vertex(Vector2f position, Color color, Vector2f texCoords)
{
Position = position;
Color = color;
TexCoords = texCoords;
}
/// <summary>
/// Provide a string describing the object
/// </summary>
/// <returns>String description of the object</returns>
public override string ToString()
{
return "[Vertex]" +
" Position(" + Position + ")" +
" Color(" + Color + ")" +
" TexCoords(" + TexCoords + ")";
}
/// <summary>2D position of the vertex</summary>
public Vector2f Position;
/// <summary>Color of the vertex</summary>
public Color Color;
/// <summary>Coordinates of the texture's pixel to map to the vertex</summary>
public Vector2f TexCoords;
}
/// <summary>
/// Define a point with color and texture coordinates
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct VertexImmu
{
/// <summary>
/// Construct the vertex from its position
/// The vertex color is white and texture coordinates are (0, 0).
/// </summary>
/// <param name="position">Vertex position</param>
public VertexImmu(Vector2f position) :
this(position, Color.White, new Vector2f(0, 0))
{
}
/// <summary>
/// Construct the vertex from its position and color
/// The texture coordinates are (0, 0).
/// </summary>
/// <param name="position">Vertex position</param>
/// <param name="color">Vertex color</param>
public VertexImmu(Vector2f position, Color color) :
this(position, color, new Vector2f(0, 0))
{
}
/// <summary>
/// Construct the vertex from its position and texture coordinates
/// The vertex color is white.
/// </summary>
/// <param name="position">Vertex position</param>
/// <param name="texCoords">Vertex texture coordinates</param>
public VertexImmu(Vector2f position, Vector2f texCoords) :
this(position, Color.White, texCoords)
{
}
/// <summary>
/// Construct the vertex from its position, color and texture coordinates
/// </summary>
/// <param name="position">Vertex position</param>
/// <param name="color">Vertex color</param>
/// <param name="texCoords">Vertex texture coordinates</param>
public VertexImmu(Vector2f position, Color color, Vector2f texCoords)
{
this.position = position;
this.color = color;
this.texCoords = texCoords;
}
/// <summary>
/// Provide a string describing the object
/// </summary>
/// <returns>String description of the object</returns>
public override string ToString()
{
return "[VertexImmu]" +
" Position(" + Position + ")" +
" Color(" + Color + ")" +
" TexCoords(" + TexCoords + ")";
}
/// <summary>2D position of the vertex</summary>
public Vector2f Position { get { return Position; } }
private readonly Vector2f position;
/// <summary>Color of the vertex</summary>
public Color Color { get { return color; } }
private readonly Color color;
/// <summary>Coordinates of the texture's pixel to map to the vertex</summary>
public Vector2f TexCoords { get { return texCoords; } }
private readonly Vector2f texCoords;
}
/// <summary>
/// Vector2f is an utility class for manipulating 2 dimensional
/// vectors with float components
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Vector2f
{
/// <summary>
/// Construct the vector from its coordinates
/// </summary>
/// <param name="x">X coordinate</param>
/// <param name="y">Y coordinate</param>
public Vector2f(float x, float y)
{
X = x;
Y = y;
}
/// <summary>
/// Operator - overload ; returns the opposite of a vector
/// </summary>
/// <param name="v">Vector to negate</param>
/// <returns>-v</returns>
public static Vector2f operator -(Vector2f v)
{
return new Vector2f(-v.X, -v.Y);
}
/// <summary>
/// Operator - overload ; subtracts two vectors
/// </summary>
/// <param name="v1">First vector</param>
/// <param name="v2">Second vector</param>
/// <returns>v1 - v2</returns>
public static Vector2f operator -(Vector2f v1, Vector2f v2)
{
return new Vector2f(v1.X - v2.X, v1.Y - v2.Y);
}
/// <summary>
/// Operator + overload ; add two vectors
/// </summary>
/// <param name="v1">First vector</param>
/// <param name="v2">Second vector</param>
/// <returns>v1 + v2</returns>
public static Vector2f operator +(Vector2f v1, Vector2f v2)
{
return new Vector2f(v1.X + v2.X, v1.Y + v2.Y);
}
/// <summary>
/// Operator * overload ; multiply a vector by a scalar value
/// </summary>
/// <param name="v">Vector</param>
/// <param name="x">Scalar value</param>
/// <returns>v * x</returns>
public static Vector2f operator *(Vector2f v, float x)
{
return new Vector2f(v.X * x, v.Y * x);
}
/// <summary>
/// Operator * overload ; multiply a scalar value by a vector
/// </summary>
/// <param name="x">Scalar value</param>
/// <param name="v">Vector</param>
/// <returns>x * v</returns>
public static Vector2f operator *(float x, Vector2f v)
{
return new Vector2f(v.X * x, v.Y * x);
}
/// <summary>
/// Operator / overload ; divide a vector by a scalar value
/// </summary>
/// <param name="v">Vector</param>
/// <param name="x">Scalar value</param>
/// <returns>v / x</returns>
public static Vector2f operator /(Vector2f v, float x)
{
return new Vector2f(v.X / x, v.Y / x);
}
/// <summary>
/// Provide a string describing the object
/// </summary>
/// <returns>String description of the object</returns>
public override string ToString()
{
return "[Vector2f]" +
" X(" + X + ")" +
" Y(" + Y + ")";
}
/// <summary>X (horizontal) component of the vector</summary>
public float X;
/// <summary>Y (vertical) component of the vector</summary>
public float Y;
}
/// <summary>
/// Utility class for manipulating 32-bits RGBA colors
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Color
{
/// <summary>
/// Construct the color from its red, green and blue components
/// </summary>
/// <param name="red">Red component</param>
/// <param name="green">Green component</param>
/// <param name="blue">Blue component</param>
public Color(byte red, byte green, byte blue) :
this(red, green, blue, 255)
{
}
/// <summary>
/// Construct the color from its red, green, blue and alpha components
/// </summary>
/// <param name="red">Red component</param>
/// <param name="green">Green component</param>
/// <param name="blue">Blue component</param>
/// <param name="alpha">Alpha (transparency) component</param>
public Color(byte red, byte green, byte blue, byte alpha)
{
R = red;
G = green;
B = blue;
A = alpha;
}
/// <summary>
/// Construct the color from another
/// </summary>
/// <param name="color">Color to copy</param>
public Color(Color color) :
this(color.R, color.G, color.B, color.A)
{
}
/// <summary>
/// Provide a string describing the object
/// </summary>
/// <returns>String description of the object</returns>
public override string ToString()
{
return "[Color]" +
" R(" + R + ")" +
" G(" + G + ")" +
" B(" + B + ")" +
" A(" + A + ")";
}
/// <summary>Red component of the color</summary>
public byte R;
/// <summary>Green component of the color</summary>
public byte G;
/// <summary>Blue component of the color</summary>
public byte B;
/// <summary>Alpha (transparent) component of the color</summary>
public byte A;
/// <summary>Predefined black color</summary>
public static readonly Color Black = new Color(0, 0, 0);
/// <summary>Predefined white color</summary>
public static readonly Color White = new Color(255, 255, 255);
/// <summary>Predefined red color</summary>
public static readonly Color Red = new Color(255, 0, 0);
/// <summary>Predefined green color</summary>
public static readonly Color Green = new Color(0, 255, 0);
/// <summary>Predefined blue color</summary>
public static readonly Color Blue = new Color(0, 0, 255);
/// <summary>Predefined yellow color</summary>
public static readonly Color Yellow = new Color(255, 255, 0);
/// <summary>Predefined magenta color</summary>
public static readonly Color Magenta = new Color(255, 0, 255);
/// <summary>Predefined cyan color</summary>
public static readonly Color Cyan = new Color(0, 255, 255);
/// <summary>Predefined (black) transparent color</summary>
public static readonly Color Transparent = new Color(0, 0, 0, 0);
}
}
}