Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ilruntime performance lua代码性能优化 #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public static void TestMandelbrot(StringBuilder sb)
sw.Start();
float total = Mandelbrot(64, 64, 4);
sw.Stop();
sb.AppendLine(string.Format("res=" + total + ", time:{0:0}", sw.ElapsedMilliseconds));
sb.AppendLine(string.Format("ilruntime: use self res=" + total + ", time:{0:0}", sw.ElapsedMilliseconds));
sw.Restart();
total = Mandelbrot2(64, 64, 4);
sw.Stop();
sb.AppendLine(string.Format("res2=" + total + ", time:{0:0}", sw.ElapsedMilliseconds));
sb.AppendLine(string.Format("ilruntime: use C# res2=" + total + ", time:{0:0}", sw.ElapsedMilliseconds));
}

static bool MandelbrotCheck(float workX, float workY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//#define XLUA_INSTALLED
#define XLUA_INSTALLED
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -12,12 +12,12 @@
using XLua;
//下面这行为了取消使用WWW的警告,Unity2018以后推荐使用UnityWebRequest,处于兼容性考虑Demo依然使用WWW
#pragma warning disable CS0618
[LuaCallCSharp]
#else
//下面这行为了取消使用WWW的警告,Unity2018以后推荐使用UnityWebRequest,处于兼容性考虑Demo依然使用WWW
#pragma warning disable CS0618
#endif

[LuaCallCSharp]
public class Performance : MonoBehaviour
{
public Button btnLoadStack;
Expand All @@ -29,6 +29,7 @@ public class Performance : MonoBehaviour
//AppDomain是ILRuntime的入口,最好是在一个单例类中保存,整个游戏全局就一个,这里为了示例方便,每个例子里面都单独做了一个
//大家在正式项目中请全局只创建一个AppDomain
AppDomain appdomain;
public string useLib = "";

System.IO.MemoryStream fs;
System.IO.MemoryStream p;
Expand All @@ -41,9 +42,9 @@ public class Performance : MonoBehaviour

private void Awake()
{
tests.Add("TestMandelbrot");
tests.Add("Test0");
tests.Add("Test1");
tests.Add("TestMandelbrot float数值计算");
tests.Add("Test0 transform set get pos");
tests.Add("Test1 transform rotation");
tests.Add("Test2");
tests.Add("Test3");
tests.Add("Test4");
Expand Down Expand Up @@ -74,6 +75,8 @@ void CreateTestButton(string testName, GameObject go)
btn.onClick.AddListener(() =>
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(useLib);
sb.AppendLine(testName);
#if UNITY_EDITOR || DEBUG
sb.AppendLine("请打包工程至非Development Build,并安装到真机再测试,编辑器中性能差异巨大,当前测试结果不具备测试意义");
#endif
Expand All @@ -94,7 +97,7 @@ public void LoadHotFixAssemblyStack()
//首先实例化ILRuntime的AppDomain,AppDomain是一个应用程序域,每个AppDomain都是一个独立的沙盒
appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
StartCoroutine(LoadHotFixAssembly());

useLib = "ilrstack";
}

public void LoadHotFixAssemblyRegister()
Expand All @@ -103,6 +106,7 @@ public void LoadHotFixAssemblyRegister()
//ILRuntimeJITFlags.JITImmediately表示默认使用寄存器VM执行所有方法
appdomain = new ILRuntime.Runtime.Enviorment.AppDomain(ILRuntimeJITFlags.JITImmediately);
StartCoroutine(LoadHotFixAssembly());
useLib = "ilrregister";
}

public void LoadLua()
Expand All @@ -116,6 +120,7 @@ public void LoadLua()
Debug.LogError("请自行安装XLua并生成xlua绑定代码后,将performance.lua复制到StreamingAssets后,解除Performace.cs第一行注释");
#endif
OnHotFixLoaded();
useLib = "xlua";
}

IEnumerator LoadHotFixAssembly()
Expand All @@ -142,21 +147,23 @@ IEnumerator LoadHotFixAssembly()
www.Dispose();

//PDB文件是调试数据库,如需要在日志中显示报错的行号,则必须提供PDB文件,不过由于会额外耗用内存,正式发布时请将PDB去掉,下面LoadAssembly的时候pdb传null即可
#if UNITY_ANDROID
www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.pdb");
#else
www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_Project.pdb");
#endif
while (!www.isDone)
yield return null;
if (!string.IsNullOrEmpty(www.error))
UnityEngine.Debug.LogError(www.error);
byte[] pdb = www.bytes;
//#if UNITY_ANDROID
// www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.pdb");
//#else
// www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_Project.pdb");
//#endif
// while (!www.isDone)
// yield return null;
// if (!string.IsNullOrEmpty(www.error))
// UnityEngine.Debug.LogError(www.error);
// byte[] pdb = www.bytes;
// www.Dispose();

fs = new MemoryStream(dll);
p = new MemoryStream(pdb);
//p = new MemoryStream(pdb);
try
{
appdomain.LoadAssembly(fs, p, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
appdomain.LoadAssembly(fs, null, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
}
catch
{
Expand Down
62 changes: 57 additions & 5 deletions ILRuntimeDemo/Assets/StreamingAssets/performance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,55 @@ end
local GameObject = CS.UnityEngine.GameObject
local Vector3 = CS.UnityEngine.Vector3
local Quaternion = CS.UnityEngine.Quaternion
local CSMandelbrotCheck = CS.Performance.MandelbrotCheck

function TestMandelbrot(sb)
TestMandelbrot1(sb)
TestMandelbrot2(sb)
end

function TestMandelbrot1(sb)
local data = 0.0
local iterations = 4
local width = 64
local height = 64
local _os_t = os.clock
local _ct = _os_t()
for i = 1, iterations do
local left = -2.1
local right = 1.0
local top = -1.3
local bottom = 1.3
local deltaX = (right - left) / width
local deltaY = (bottom - top) / height
local coordinateX = left
for x = 1, width do
local coordinateY = top
for y = 1, height do
local workX = 0
local workY = 0
local counter = 0
while(counter < 255 and MandelbrotCheck(workX, workY))
--while(counter < 255 and CSMandelbrotCheck(workX, workY))
do
counter = counter + 1
local newX = (workX * workX) - (workY * workY) + coordinateX
workY = 2 * workX * workY + coordinateY
workX = newX;
end

data = workX + workY
coordinateY = coordinateY + deltaY
end
coordinateX = coordinateX + deltaX
end
end
local _d = _os_t() - _ct
sb:AppendLine("lua use self res="..data..", time:".._d*1000);
end


function TestMandelbrot2(sb)
local data = 0.0
local iterations = 4
local width = 64
Expand All @@ -26,7 +74,7 @@ function TestMandelbrot(sb)
local workY = 0
local counter = 0
--while(counter < 255 and MandelbrotCheck(workX, workY))
while(counter < 255 and CS.Performance.MandelbrotCheck(workX, workY))
while(counter < 255 and CSMandelbrotCheck(workX, workY))
do
counter = counter + 1
local newX = (workX * workX) - (workY * workY) + coordinateX
Expand All @@ -41,16 +89,19 @@ function TestMandelbrot(sb)
end
end
local _d = _os_t() - _ct
sb:AppendLine("res="..data..", time:".._d*1000);
sb:AppendLine("lua use C# res="..data..", time:".._d*1000);
end

function Test0(sb)
local t = os.clock()
local go = GameObject("t")
local transform = go.transform

local x,y,z = 1

for i = 1, 2000000 do
transform.position = transform.position
x,y,z = transform:GetPosition(x,y,z)
transform:SetPosition(x,y,z)
--transform.position = transform.position
end
GameObject.Destroy(go)
sb:AppendLine("time="..(os.clock()-t)*1000)
Expand All @@ -62,7 +113,8 @@ function Test1(sb)
local transform = go.transform

for i = 1, 2000000 do
transform:Rotate(Vector3.up, 1)
transform:Rotation(0, 1, 0, 1)
--transform:Rotate(Vector3.up, 1)
end

GameObject.Destroy(go)
Expand Down
Loading