웹 및이 사이트에서 몇 시간 동안이 질문에 대한 답변을 찾으려고 노력 중입니다. 그곳에.스택 크기 변경을 피하고 C#에서 스택 오버플로가 발생하지 않도록하려면 어떻게해야합니까?
.NET은 앱에 1MB를 할당하며 스택 크기를 강요하는 대신 다시 코딩하여 스택 오버플로를 방지하는 것이 가장 좋습니다.
나는 약 3000 개의 노드까지 훌륭하게 작동하는 "최단 경로"응용 프로그램을 만들고 있습니다.이 응용 프로그램은 오버플로가됩니다. 여기서 문제가 발생시키는 방법이다 :
public Dictionary<int, int> edges = new Dictionary<int, int>();
그래프 []이고 : I는를 opimize 시도한
private Dictionary<int, Node> graph = new Dictonary<int, Node>();
참고로
public void findShortestPath(int current, int end, int currentCost)
{
if (!weight.ContainsKey(current))
{
weight.Add(current, currentCost);
}
Node currentNode = graph[current];
var sortedEdges = (from entry in currentNode.edges orderby entry.Value ascending select entry);
foreach (KeyValuePair<int, int> nextNode in sortedEdges)
{
if (!visited.ContainsKey(nextNode.Key) || !visited[nextNode.Key])
{
int nextNodeCost = currentCost + nextNode.Value;
if (!weight.ContainsKey(nextNode.Key))
{
weight.Add(nextNode.Key, nextNodeCost);
}
else if (weight[nextNode.Key] > nextNodeCost)
{
weight[nextNode.Key] = nextNodeCost;
}
}
}
visited.Add(current, true);
foreach (KeyValuePair<int, int> nextNode in sortedEdges)
{
if(!visited.ContainsKey(nextNode.Key) || !visited[nextNode.Key]){
findShortestPath(nextNode.Key, end, weight[nextNode.Key]);
}
}
}//findShortestPath
를 상기 Node 클래스는 하나의 부재를 가지고 코드를 사용하여 하나의 반복 (재귀?)에서 필요한 것보다 더 많은 수하물을 운반 할 수 없지만 각 노드의 1-9 가장자리 사이에 100K 노드 그래프를 사용하면 1MB 제한에 도달하는 데 걸리는 시간이 매우 단축됩니다.
어쨌든 누구든지 내게 포인터 (not like this)를 주면 C# 및 코드 최적화에 익숙하지 않아서 고맙겠습니다.
또는 CLR을 사용하여 알고리즘을 꼬리 재귀 형식으로 변환 할 수 있는지 확인할 수 있습니다. – LBushkin
C#이 꼬리 호출 명령을 생성하지 않습니다. 특정 버전의 지터는 tailcall 명령어가 사용되지 않아도 꼬리 재귀를 사용하여 특정 메소드를 최적화 할 수 있음을 알 수 있습니다. 그러나 우리가 발표 한 대부분의 불안감에는이 최적화가 없으므로이 최적화에 의존해서는 안됩니다. –
게다가, 당신의 충고는 실제로 실행 가능하지 않습니다. * 원래 포스터는 "알고리즘을 꼬리 재귀 양식으로 변환 할 수 있는지 확인"하는 방법이 정확히 무엇입니까? 이것은 런타임의 구현 세부 사항에 대한 깊은 이해를 필요로하는 복잡한 프로세스입니다. 빌딩 42에있는 사람 이외에 다른 사람이 소유 할 것이라고는 예상하지 않습니다. –