13 Sekunden habe ich gezählt um folgendes Script 1000 mal aufzurufen um die Werte vorzuberechnen:
Code: Alles auswählen
Lua lua = new Lua();
lua.State.Encoding = Encoding.UTF8;
lua.DoString(@"
function bullet (result, time)
result.x = 100*time
result.y = 40*math.sin(time*4)
end
");
var scriptFunc = lua["bullet"] as LuaFunction;
float time = 0;
for(var i = 0; i < accuracy; i++, time += step)
{
var r = rarr[i]; r.x = 0; r.y = 0; r.a = 0; r.s = 1;
scriptFunc.Call(r, time);
}
Ist nicht gerade schnell. Ich muss es wohl noch mal anders versuchen. Mit dieser Funktion wollte ich mir in einem Aufruf die 1000 Werte berechnen lassen.
Code: Alles auswählen
function all()
a = {}
steps = 1000
step = 10.0/steps
for i=0,steps do
result = {x=0,y=0,a=0,s=1}
bullet(result, 1)
table.insert(a,result);
end
return a
end
Es gibt ein LuaTable zurück. Der Code der danach entstanden ist, ist ziemlich ekelhaft. Deshalb bin ich zu den 1000 Aufrufen zurück gewechselt.
Aber es läuft mit diesem Ekelcode schneller, aber immer noch viel zu langsam. Je mehr Zeilen ausgeführt werden, desto langsamer. Scheint fast linear zu sein. Für 4 Zeilen 12s, für eine ~3s... mach ich was voll falsch?
Code: Alles auswählen
lua.DoString(@"
function bullet (result, time)
" + code + @"
end
function all()
a = {}
time = 0
steps = "+accuracy+@"
step = 10.0/steps
for i=0,steps-1 do
result = {x=0,y=0,a=0,s=1}
bullet(result, time)
table.insert(a,result);
time = time+step
end
return a
end
");
var scriptFunc = lua["all"] as LuaFunction;
var table = scriptFunc.Call()[0] as LuaTable;
Dictionary<object, object> dict = lua.GetTableDict(table);
int i = 0;
try
{
foreach (KeyValuePair<object, object> de in dict)
{
Console.WriteLine("{0} {1}", de.Key.ToString(), de.Value.ToString());
var r = rarr[i++];
Dictionary<object, object> tmp = lua.GetTableDict(de.Value as LuaTable);
object value;
tmp.TryGetValue("x", out value);
r.x = Double.Parse(value.ToString());
tmp.TryGetValue("y", out value);
r.y = Double.Parse(value.ToString());
tmp.TryGetValue("a", out value);
r.a = Double.Parse(value.ToString());
tmp.TryGetValue("s", out value);
r.s = Double.Parse(value.ToString());
}
}catch(Exception ex)
{
Debug.LogError(ex);
}
Code: Alles auswählen
result.x = 100*time+50
result.a = time
result.s = math.sin(time*6)
result.y = math.sin(time)*60
edit:
http://mathparser.org/ schaut vielversprechend aus