Simple XNA 2D demo: why is my F# version slower than C# version?

Posted by Den on Stack Overflow See other posts from Stack Overflow or by Den
Published on 2012-06-18T21:14:11Z Indexed on 2012/06/18 21:15 UTC
Read the original article Hit count: 266

When running this XNA application it should display a rotated rectangle that moves from top-left corner to bottom-right corner.

It looks like my F# version is noticeably much slower. It seems that the Draw method skips a lot of frames.

I am using VS 2012 RC, XNA 4.0, .NET 4.5, F# 3.0. I am trying to make it as functional as possible.

What could be the reason for poor performance?

C#:

class Program
{
    static void Main(string[] args)
    {
        using (var game = new FlockGame())
        {
            game.Run();
        }
    }
}

public class FlockGame : Game
{
    private GraphicsDeviceManager graphics;
    private DrawingManager drawingManager;
    private Vector2 position = Vector2.Zero;

    public FlockGame()
    {
        graphics = new GraphicsDeviceManager(this);
    }

    protected override void Initialize()
    {
        drawingManager = new DrawingManager(graphics.GraphicsDevice);
        this.IsFixedTimeStep = false;
    }

    protected override void Update(GameTime gameTime)
    {
        position = new Vector2(position.X + 50.1f * (float)gameTime.ElapsedGameTime.TotalSeconds,
                               position.Y + 50.1f * (float)gameTime.ElapsedGameTime.TotalSeconds);
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        //this.GraphicsDevice.Clear(Color.Lavender)

        drawingManager.DrawRectangle(position, new Vector2(100.0f, 100.0f), 0.7845f, Color.Red);

        base.Draw(gameTime);
    }
}

public class DrawingManager
{
    private GraphicsDevice GraphicsDevice;
    private Effect Effect;

    public DrawingManager(GraphicsDevice graphicsDevice)
    {
        GraphicsDevice = graphicsDevice;
        this.Effect =
            new BasicEffect(this.GraphicsDevice)
                {
                    VertexColorEnabled = true,
                    Projection = Matrix.CreateOrthographicOffCenter(0.0f, this.GraphicsDevice.Viewport.Width,
                                                                    this.GraphicsDevice.Viewport.Height,                                                                         0.0f, 0.0f, 1.0f)
                };
    }

    private VertexPositionColor[] GetRectangleVertices (Vector2 center, Vector2 size, float radians, Color color)
    {
        var halfSize = size/2.0f;
        var topLeft = -halfSize;
        var bottomRight = halfSize;
        var topRight = new Vector2(bottomRight.X, topLeft.Y);
        var bottomLeft = new Vector2(topLeft.X, bottomRight.Y);

        topLeft = Vector2.Transform(topLeft, Matrix.CreateRotationZ(radians)) + center;
        topRight = Vector2.Transform(topRight, Matrix.CreateRotationZ(radians)) + center;
        bottomRight = Vector2.Transform(bottomRight, Matrix.CreateRotationZ(radians)) + center;
        bottomLeft = Vector2.Transform(bottomLeft, Matrix.CreateRotationZ(radians)) + center;

        return new VertexPositionColor[]
        {
            new VertexPositionColor(new Vector3(topLeft, 0.0f), color),
            new VertexPositionColor(new Vector3(topRight, 0.0f), color),
            new VertexPositionColor(new Vector3(topRight, 0.0f), color),
            new VertexPositionColor(new Vector3(bottomRight, 0.0f), color),
            new VertexPositionColor(new Vector3(bottomRight, 0.0f), color),
            new VertexPositionColor(new Vector3(bottomLeft, 0.0f), color),
            new VertexPositionColor(new Vector3(bottomLeft, 0.0f), color),
            new VertexPositionColor(new Vector3(topLeft, 0.0f), color)
        };
    }


    public void DrawRectangle(Vector2 center, Vector2 size, float radians, Color color)
    {
        var vertices = GetRectangleVertices(center, size, radians, color);

        foreach (var pass in this.Effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            this.GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, vertices.Length/2);
        }
    }
}

F#:

namespace Flocking

module FlockingProgram = 
    open System
    open Flocking

    [<STAThread>]
    [<EntryPoint>]
    let Main _ =
        use g = new FlockGame()
        g.Run()
        0

//------------------------------------------------------------------------------

namespace Flocking

open System
open System.Diagnostics
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input


type public FlockGame() as this =
    inherit Game()
    let mutable graphics = new GraphicsDeviceManager(this)
    let mutable drawingManager = null
    let mutable position = Vector2.Zero

    override Game.LoadContent() =
        drawingManager <- new Rendering.DrawingManager(graphics.GraphicsDevice)
        this.IsFixedTimeStep <- false

    override Game.Update gameTime =
        position <- Vector2(position.X + 50.1f * float32 gameTime.ElapsedGameTime.TotalSeconds,
                            position.Y + 50.1f * float32 gameTime.ElapsedGameTime.TotalSeconds)
        base.Update gameTime

    override Game.Draw gameTime =
        //this.GraphicsDevice.Clear(Color.Lavender)

        Rendering.DrawRectangle(drawingManager, position, Vector2(100.0f, 100.0f), 0.7845f, Color.Red)

        base.Draw gameTime

//------------------------------------------------------------------------------

namespace Flocking

open System
open System.Collections.Generic
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input

module Rendering = 

    [<AllowNullLiteral>]
    type DrawingManager (graphicsDevice : GraphicsDevice) = 
        member this.GraphicsDevice = graphicsDevice
        member this.Effect =
            new BasicEffect(this.GraphicsDevice,
                VertexColorEnabled = true,
                Projection = Matrix.CreateOrthographicOffCenter(0.0f,
                    float32 this.GraphicsDevice.Viewport.Width,
                    float32 this.GraphicsDevice.Viewport.Height,
                    0.0f, 0.0f, 1.0f))


    let private GetRectangleVertices (center:Vector2, size:Vector2, radians:float32, color:Color) = 
        let halfSize = size / 2.0f
        let mutable topLeft = -halfSize
        let mutable bottomRight = halfSize
        let mutable topRight = new Vector2(bottomRight.X, topLeft.Y)
        let mutable bottomLeft = new Vector2(topLeft.X, bottomRight.Y)

        topLeft <- Vector2.Transform(topLeft, Matrix.CreateRotationZ(radians)) + center
        topRight <- Vector2.Transform(topRight, Matrix.CreateRotationZ(radians)) + center
        bottomRight <- Vector2.Transform(bottomRight, Matrix.CreateRotationZ(radians)) + center
        bottomLeft <- Vector2.Transform(bottomLeft, Matrix.CreateRotationZ(radians)) + center

        [|
            new VertexPositionColor(new Vector3(topLeft, 0.0f), color)
            new VertexPositionColor(new Vector3(topRight, 0.0f), color)
            new VertexPositionColor(new Vector3(topRight, 0.0f), color)
            new VertexPositionColor(new Vector3(bottomRight, 0.0f), color)
            new VertexPositionColor(new Vector3(bottomRight, 0.0f), color)
            new VertexPositionColor(new Vector3(bottomLeft, 0.0f), color)
            new VertexPositionColor(new Vector3(bottomLeft, 0.0f), color)
            new VertexPositionColor(new Vector3(topLeft, 0.0f), color)
        |]


    let DrawRectangle (drawingManager:DrawingManager, center:Vector2, size:Vector2, radians:float32, color:Color) = 
        let vertices = GetRectangleVertices(center, size, radians, color)

        for pass in drawingManager.Effect.CurrentTechnique.Passes do
            pass.Apply()
            drawingManager.GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, vertices.Length/2)

© Stack Overflow or respective owner

Related posts about Performance

Related posts about F#