Skip to content

Instantly share code, notes, and snippets.

@ZeroStride
Created April 5, 2010 01:30
Show Gist options
  • Save ZeroStride/355889 to your computer and use it in GitHub Desktop.
Save ZeroStride/355889 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
// AoS (class) 28-bytes
// this test uses an array of particle classes 23-bytes wide and processes each particle individually
namespace XNA360MathTest
{
static class Program
{
class ParticleClass
{
public Vector3 pos;
public Vector3 vel;
public float timeremaining;
}
static unsafe void Main(string[] args)
{
const int NumParticles = 100000;
const float dt = 0.04f;
const int NumSimulationRuns = 50;
ParticleClass[] particle_stream = new ParticleClass[NumParticles];
Random rnd = new Random();
for (int i = 0; i < NumParticles; i++)
{
particle_stream[i] = new ParticleClass();
particle_stream[i].pos.X = (float)rnd.NextDouble();
particle_stream[i].pos.Y = (float)rnd.NextDouble();
particle_stream[i].pos.Z = (float)rnd.NextDouble();
particle_stream[i].vel.X = (float)rnd.NextDouble();
particle_stream[i].vel.Y = (float)rnd.NextDouble();
particle_stream[i].vel.Z = (float)rnd.NextDouble();
particle_stream[i].timeremaining = (float)rnd.NextDouble();
}
Stopwatch hptimer = new Stopwatch();
float simAvg = 0.0f;
float simMin = float.PositiveInfinity;
float simMax = 0.0f;
Vector3 dpdt;
Vector3 dt_v = new Vector3(dt);
for (int j = 0; j < NumSimulationRuns; j++)
{
hptimer.Reset();
hptimer.Start();
for (int i = 0; i < NumParticles; i++)
{
Vector3.Multiply(ref particle_stream[i].vel, ref dt_v, out dpdt);
Vector3.Add(ref particle_stream[i].pos, ref dpdt, out particle_stream[i].pos);
particle_stream[i].timeremaining -= dt;
}
hptimer.Stop();
float elapsedMS = hptimer.ElapsedTicks * 1000.0f / (float)Stopwatch.Frequency;
simAvg += elapsedMS;
simMin = (simMin > elapsedMS ? elapsedMS : simMin);
simMax = (simMax < elapsedMS ? elapsedMS : simMax);
}
simAvg /= NumSimulationRuns;
string outpt = "Average: " + simAvg + " Min: " + simMin + " Max: " + simMax;
Console.WriteLine(outpt); // Set breakpoint here
}
}
}
using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
// AoS (class) 28-bytes
// this test uses an array of particle classes 23-bytes wide and processes each particle by calling its Update method
namespace XNA360MathTest
{
static class Program
{
class ParticleClass
{
public Vector3 pos;
public Vector3 vel;
public float timeremaining;
public void Update(ref Vector3 dt_v, ref Vector3 dpdt)
{
Vector3.Multiply(ref vel, ref dt_v, out dpdt);
Vector3.Add(ref pos, ref dpdt, out pos);
timeremaining -= dt_v.X;
}
}
static unsafe void Main(string[] args)
{
const int NumParticles = 3200000;
const float dt = 0.04f;
const int NumSimulationRuns = 50;
ParticleClass[] particle_stream = new ParticleClass[NumParticles];
Random rnd = new Random();
for (int i = 0; i < NumParticles; i++)
{
particle_stream[i] = new ParticleClass();
particle_stream[i].pos.X = (float)rnd.NextDouble();
particle_stream[i].pos.Y = (float)rnd.NextDouble();
particle_stream[i].pos.Z = (float)rnd.NextDouble();
particle_stream[i].vel.X = (float)rnd.NextDouble();
particle_stream[i].vel.Y = (float)rnd.NextDouble();
particle_stream[i].vel.Z = (float)rnd.NextDouble();
particle_stream[i].timeremaining = (float)rnd.NextDouble();
}
Stopwatch hptimer = new Stopwatch();
float simAvg = 0.0f;
float simMin = float.PositiveInfinity;
float simMax = 0.0f;
Vector3 dpdt = new Vector3();
Vector3 dt_v = new Vector3(dt);
for (int j = 0; j < NumSimulationRuns; j++)
{
hptimer.Reset();
hptimer.Start();
for (int i = 0; i < NumParticles; i++)
{
particle_stream[i].Update(ref dt_v, ref dpdt);
}
hptimer.Stop();
float elapsedMS = hptimer.ElapsedTicks * 1000.0f / (float)Stopwatch.Frequency;
simAvg += elapsedMS;
simMin = (simMin > elapsedMS ? elapsedMS : simMin);
simMax = (simMax < elapsedMS ? elapsedMS : simMax);
}
simAvg /= NumSimulationRuns;
string outpt = "Average: " + simAvg + " Min: " + simMin + " Max: " + simMax;
Console.WriteLine(outpt); // Set breakpoint here
}
}
}
using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
// AoS (class) 28-bytes
// this test uses an array of particle classes 23-bytes wide and processes each particle by calling its virtual Update method
namespace XNA360MathTest
{
static class Program
{
class ParticleClass
{
public Vector3 pos;
public Vector3 vel;
public float timeremaining;
public virtual void Update(ref Vector3 dt_v, ref Vector3 dpdt)
{
Vector3.Multiply(ref vel, ref dt_v, out dpdt);
Vector3.Add(ref pos, ref dpdt, out pos);
timeremaining -= dt_v.X;
}
}
static unsafe void Main(string[] args)
{
const int NumParticles = 100000;
const float dt = 0.04f;
const int NumSimulationRuns = 50;
ParticleClass[] particle_stream = new ParticleClass[NumParticles];
Random rnd = new Random();
for (int i = 0; i < NumParticles; i++)
{
particle_stream[i] = new ParticleClass();
particle_stream[i].pos.X = (float)rnd.NextDouble();
particle_stream[i].pos.Y = (float)rnd.NextDouble();
particle_stream[i].pos.Z = (float)rnd.NextDouble();
particle_stream[i].vel.X = (float)rnd.NextDouble();
particle_stream[i].vel.Y = (float)rnd.NextDouble();
particle_stream[i].vel.Z = (float)rnd.NextDouble();
particle_stream[i].timeremaining = (float)rnd.NextDouble();
}
Stopwatch hptimer = new Stopwatch();
float simAvg = 0.0f;
float simMin = float.PositiveInfinity;
float simMax = 0.0f;
Vector3 dpdt = new Vector3();
Vector3 dt_v = new Vector3(dt);
for (int j = 0; j < NumSimulationRuns; j++)
{
hptimer.Reset();
hptimer.Start();
for (int i = 0; i < NumParticles; i++)
{
particle_stream[i].Update(ref dt_v, ref dpdt);
}
hptimer.Stop();
float elapsedMS = hptimer.ElapsedTicks * 1000.0f / (float)Stopwatch.Frequency;
simAvg += elapsedMS;
simMin = (simMin > elapsedMS ? elapsedMS : simMin);
simMax = (simMax < elapsedMS ? elapsedMS : simMax);
}
simAvg /= NumSimulationRuns;
string outpt = "Average: " + simAvg + " Min: " + simMin + " Max: " + simMax;
Console.WriteLine(outpt); // Set breakpoint here
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment