게임의 데이터 텍스트를로드하고 구문 분석 할 코드를 작성하고 있습니다. 분기 리더 호출이 많이 있습니다. 예를 들어지도에 소품이나 적과 같은 데이터가 포함되어 있고 해당 오브젝트에는 호출되어 읽히는 파일이 있습니다. 일반적으로이 모든 데이터는 빠르게로드됩니다. 약 1.5 초 느린 컴퓨터에서는 약 초 이상 걸릴 수 있고 게임 창이 끝날 때까지 응답하지 않게 할 수 있습니다.비동기 데이터 읽기/구문 분석 성능
지금 당장은로드 시간을 줄이면서 창을 활성 상태로 유지하는 방법을 모색 중입니다. 로드 중 일부를 메인 스레드의 백그라운드에서 실행되는 태스크로 분리 한 다음로드가 완료되면 메인 스레드에게 상태를 전환하도록 알려주고 게임이 계속됩니다. 이것은 작동하지만, 나는 1.5 두 번째 로딩 시간부터 두 번째로로 이동했습니다. 이런 백그라운드 작업으로 전환 할 때 정상적인 성능입니까? 현재 처리되는 방법의 예로서 일반화 된 코드를 게시했습니다.
Map map = null;
//Main Update Loop
public void Update()
{
if(GameState == Active)
map.Update();
else
ShowLoadingScreen();
}
//LoadWorld gets called from elsewhere, like a UI
public async void LoadWorld()
{
GameState = State.Loading;
await Task.Run(() => { LoadFile("mapdata", out map); });
GameState = State.Active;
map.Start();
}
//This loads the file and reads the first line
//which tells the reader what sort of object it is
public void LoadFile(String file, out Map m)
{
m = new Map(); //create new map
StreamReader sr = new StreamReader(file);
String line;
Object obj = null;
while ((line = sr.ReadLine()) != null)
{
switch(line)
{
case "A":
obj = parseObjectA(line, sr); //continues with its own loop
break;
case "B":
obj = parseObjectB(line, sr); //continues with its own loop
break;
}
map.addObject(obj);
}
}
//This loops through the reader and fills an object with data, then returns it
public Object parseObjectA(String line, StreamReader sr)
{
Object obj = new Object();
while ((line = sr.ReadLine()) != null)
{
String element;
String value;
//parseLine is a function that breaks apart the line into an element and value
parseLine(line, out element, out value);
switch(element)
{
case "name":
obj.Name = value;
break;
case "position":
{
int pos = 0;
Int32.TryParse(value, out pos);
obj.position = pos;
break;
}
}
}
return obj;
}
나에게 보인다. 스트림 리더의 사용법을'using' 블록으로 묶는 것도 고려해보십시오. 성능에 영향을 줄 수있는 사항이 있는지 계속 읽습니다. – Fabulous
@Fabulous ReadLine은 I/O 작업이므로 스레드를 차단하지 않으며 ReadLineAsync는 비동기 적으로 수행 할 필요가 없기 때문에 ReadLineAsync가 성능을 더욱 저하시킬 수 있으므로 ReadLineAsync를 사용하면 불필요한 작업이 추가됩니다 간접비. 그게 사실인가요? – DangerMcD
되돌아 오는 데이터를 처리 할 때 그 차이가 미미할 수 있습니다. [이 스레드] 고려 (https://social.msdn.microsoft.com/Forums/vstudio/en-US/a47412ed-1b21-45c2-8554-cc602d6b5ddb/is-streamreaderreadlineasync-much-slower-than-readline?forum= 평행 안정성). 그곳에는 다른 측면이 있지만 귀하의 질문은 다루어집니다. 일반적으로'async/await'는 호출 스택의 모든 단계를 캐스케이드 호출합니다. 이 경우, 당신은'parseLine' 연산에서 코드를 보길 원할 것입니다. – Fabulous