简单的XNA 2D演示:为什么我的F#版本比C#版本慢?

时间:2012-06-18 21:14:11

标签: performance f# xna functional-programming c#-to-f#

运行此XNA应用程序时,它应显示一个从左上角移动到右下角的旋转矩形。

看起来我的F#版本明显慢得多。似乎Draw方法跳过很多帧。

我正在使用VS 2012 RC,XNA 4.0,.NET 4.5,F#3.0。我试图让它尽可能地发挥作用。

表现不佳可能是什么原因?

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)

1 个答案:

答案 0 :(得分:16)

这是猜测;我对此域一无所知,但Effect属性每次读取时都会创建一个新的BasicEffect对象;也许你想在构造函数中使用let初始化该对象一次,然后返回该对象?

相关问题