In this page we show Casanova performance evaluation with respect to other languages used in game development and we provide the source code written in each language.

Evaluation:
performance_table.png

Tested on Dual-core 1.9 GHz Intel Core i5-4300U 4 gb ddr3-1600

Casanova code
module Game
open "UnityEngine.dll"
open UnityEngine

scene World =
  { 
    Balls : [Ball]    
    Create() =
      {
        Balls = [for i in [1..10000] do 
                 select (Ball.Create())]
        
      }
  }

entity Ball =
  {
    P : Vector2
    V : Vector2

    rule P = P + V
    rule V = 
      wait P.x >= 500.0f
      yield V * -1.0f
      wait P.x <= -500.0f
      yield V * -1.0f

    Create() =
      {
        P = Vector2(0.0f,0.0f)
        V = Vector2(0.1f,0.0f)
      }
  }


C# code
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
namespace CSharpIenumerable
{
  class Ball
  {
    public Vector2 P { get; set; }
    public Vector2 V { get; set; }

    public Ball()
    {
      P = Vector2.zero;
      V = Vector2.zero;
      p_rule = PRule();
      v_rule = VRule();
    }

    IEnumerator PRule()
    {
      while (true)
      {
        P = P + V;
        yield return null;
      }
    }
    IEnumerator VRule()
    {
      while (true)
      {
        while (P.x <= 500) yield return null;
        P = new Vector2(500, P.y);
        V *= -1.0f;
        yield return null;
        while (P.x > -500) yield return null;
        P = new Vector2(-500, P.y);
        V *= -1.0f;
        yield return null;
      }
    }

    public IEnumerator p_rule, v_rule;

    public void Update(float dt)
    {
      p_rule.MoveNext();
      v_rule.MoveNext();
    }
  }

  class Game
  {
    List<Ball> balls = new List<Ball>();
    public Game()
    {
      for (int i = 0; i < 10000; i++)
      {
        balls.Add(new Ball());
      }
    }

    public void Update(float dt)
    {
      for (int i = 0; i < balls.Count; i++)
      {
        balls[i].Update(dt);
      }
    }
  }
}




Javascript code
function Vector2(x, y){
   this.x = x;
   this.y = y;
}

function Ball(){
	this.P = new Vector2(0.0,0.0);
	this.V = new Vector2(0.1,0.0);
	this.P_rule_iterator = P_rule(this);
	this.V_rule_iterator = V_rule(this);
};

function* P_rule(elem) {
		while(true){
				yield 0;
				elem.P = new Vector2(elem.P.x + elem.V.x, elem.P.y + elem.V.y);			
			}
	}
	
function* V_rule(elem) {
		while (true) {
			yield 0;
			if(elem.P.x > 500.0){				
				elem.P = new Vector2(500, elem.P.y);
				elem.V = new Vector2(-0.1,0.0);
			}
			if(elem.P.x < -500){				
				elem.P = new Vector2(-500, elem.P.y);
				elem.V = new Vector2(0.1,0.0);
			}
		}
	}

var balls = new Array ();
var balls_number = 10000;
var iterations = 10000;
for (var i = 0; i < balls_number; i++) {
	var b = new Ball();
	balls.push(b);
}

for (var i = 0; i < iterations; i++) {
	for (var bi = 0; bi < balls_number; bi++) {
		balls[bi].P_rule_iterator.next();
		balls[bi].V_rule_iterator.next();
	}
}


Lua code
Vector2 = {}
Vector2.__index = Vector2
function Vector2.create(x,y)
   local acnt = {}             -- our new object
   setmetatable(acnt,Vector2)  -- make Account handle lookup
   acnt.x = x
   acnt.y = y
   return acnt
end


Ball = {}
Ball.__index = Ball
function Ball.create()
   local acnt = {}             -- our new object
   setmetatable(acnt,Ball)  -- make Account handle lookup
   acnt.p = Vector2.create(0.0,0.0)
   acnt.v = Vector2.create(0.1,0.0)

   acnt.p_rule =
	coroutine.create(function ()
		while true do
            coroutine.yield()
			acnt.p = Vector2.create(acnt.p.x + acnt.v.x, acnt.p.y + acnt.v.y)
		end
	end)

   acnt.v_rule =
	coroutine.create(function ()
		while true do
			coroutine.yield()
			if acnt.p.x > 500 then
				acnt.p = Vector2(500, acnt.p.y)
				acnt.v = Vector2(-0.1, 0.0)
			elseif acnt.p.x < -500 then
				acnt.p = Vector2(-500, acnt.p.y)
				acnt.v = Vector2(0.1, 0.0)
			end
		end
	end)

   return acnt
end



balls = {}


for var=0,10000 do
	table.insert(balls, Ball.create())
end
function update(_index)
	coroutine.resume(balls[_index].p_rule)
	coroutine.resume(balls[_index].v_rule)
end

for var=0,10000 do
	table.foreach(balls, update)
end


Python code
import time

class Vector2(object):
    x = 0.0
    y = 0.0
    def __init__(self, x1, y1):
        self.x = x1
        self.y = y1

class Ball(object):
    p = Vector2(0.0,0.0)
    v = Vector2(0.1,0.0)
	
    def p_rule(self):
        while True:
            yield 0
            self.p = Vector2(self.p.x + self.v.x, self.p.y + self.v.y)
    def v_rule(self):
        while True:
            yield 0
            if(self.p.x > 500):
                self.p = Vector2(500, self.p.y)			
                self.v = Vector2(-0.1,0.0)
            if(self.p.x < -500):
                self.p = Vector2(-500, self.p.y)
                self.v = Vector2(0.1,0.0)

class BallIterator:
    ball = Ball()
    p_iterator = ball.p_rule()
    v_iterator = ball.v_rule()


balls = []

for i in range(10000):
    balls.append(BallIterator())

for i in range(10000):
    for b in balls:       
        next(b.p_iterator)
        next(b.v_iterator)

Last edited Jan 13, 2015 at 6:13 PM by Ext3rmin4tor, version 11

Comments

No comments yet.