我们游戏中第一次运行中,会下载配置表并进行加载。
但是我们发现一个现象:
第一次下载后,会在加载那里卡5秒左右。而以后再也不会遇到相同的卡的状况。
花了些时间发现了原因是c#代码导致的。
我们配置表使用csv,第一次下载后通过www类可以拿到下载的byte[],想避免一次IO操作,就使用c#写了解析为字符串数组的代码。
而非第一次下载,则使用File.ReadAllLine直接从文件中读取。
原来写时,以为能避免一次IO操作,可以提高性能,这里不能不说,C#自带的函数(底层使用c/c++实现),要远比c#的代码速度快的多。
也有可能是我写的C#代码太烂了的原因。
烂代码大家可以了解下:就是被注释掉的代码引导的卡。
string[] allDatas = null;
/*
if (doneList != null)
{
for (int m = 0; m < doneList.Count; m++)
{
ConfigFile cf = doneList[m];
if (cf.configName.Equals(configName+".bin"))
{
Debug.Log("Time1 :"+Time.realtimeSinceStartup);
StringReader sr = new StringReader(UTF8Encoding.UTF8.GetString(cf.content));
List<string> list = new List<string>();
string str = null;
while ((str = sr.ReadLine()) != null)
{
list.Add(str);
}
Debug.Log("Time2:"+Time.realtimeSinceStartup);
allDatas = list.ToArray();
Debug.Log("Time3:"+Time.realtimeSinceStartup+" "+configName+" "+list.Count);
break;
}
}
}
*/
if (allDatas == null)
{
string path = PathMgr.Config_Path + configName + ".bin";
allDatas = File.ReadAllLines(path);
}
0 条评论。