为什么 MapPoint.Route.Calculate() 比应用程序本身的计算慢得多?
我刚刚尝试使用mappoint api 通过代码进行多重路由。它工作得很好,但有一些路线需要很长时间才能计算(18.5 秒)。如果我通过 MapPoint 本身计算相同的路线,则只需不到 2 秒。
在上面的示例中,我计算了德国乌尔姆和曼海姆之间的路线。
class CalculateTime : IDisposable
{
MapPoint.Application app;
MapPoint.Map map;
MapPoint.Route route;
public CalculateTime()
{
app = (MapPoint.Application)Activator.CreateInstance(Type.GetTypeFromProgID("mappoint.application"));
map = app.ActiveMap;
route = map.ActiveRoute;
}
public class Place
{
public string City { get; set; }
public string Street { get; set; }
public string Postal { get; set; }
public Place(string City = "", string Street = "", string Postal = "")
{
this.City = City;
this.Street = Street;
this.Postal = Postal;
}
}
public class Place
{
public string City { get; set; }
public string Street { get; set; }
public string Postal { get; set; }
public Place(string City = "", string Street = "", string Postal = "")
{
this.City = City;
this.Street = Street;
this.Postal = Postal;
}
}
public TimeSpan Calculate(Place From, Place To)
{
Stopwatch sw = new Stopwatch();
MapPoint.FindResults frFromCollection = map.FindAddressResults(City: From.City, Street: From.Street, PostalCode: From.Postal);
MapPoint.FindResults frToCollection = map.FindAddressResults(City: To.City, Street: To.Street, PostalCode: To.Postal);
object frFrom = frFromCollection[1];
object frTo = frToCollection[1];
route.Waypoints.Add(frFrom);
route.Waypoints.Add(frTo);
sw.Start();
route.Calculate();
TimeSpan time = new TimeSpan(0, (int)(route.DrivingTime * 24 * 60), 0);
route.Clear();
sw.Stop();
Marshal.ReleaseComObject(frFromCollection);
Marshal.ReleaseComObject(frToCollection);
Marshal.ReleaseComObject(frFrom);
Marshal.ReleaseComObject(frTo);
MessageBox.Show(sw.Elapsed.Seconds + "." + sw.ElapsedMilliseconds);
return time;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~CalculateTime()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
map.Saved = true;
Marshal.ReleaseComObject(route);
Marshal.ReleaseComObject(map);
app.Quit();
Marshal.ReleaseComObject(app);
}
}
}
我记得有另一种方法来计算显示弹出窗口的路线(不是 MapPoint.Route.Calculate()
,但我忘了。
有人知道如何加快计算速度吗?
问候 哇
编辑: 我刚刚尝试过这个: 通过代码添加航点,但通过 MapPoint 本身计算路线。这也需要很长时间。不知怎的,似乎航点是问题,而不是计算
方法
编辑:
它看起来像MapPoint已经在后台计算路线。 如果我在调用计算之前等待几秒钟,它会在几毫秒内完成。
i just tried multiple routing via code with the mappoint api. It works all fine, but a few routes takes extrem long to calculate (18.5 sec). If i calculate the same route via MapPoint itself it only takes less than 2 sec.
In the example above i calculated the route between Ulm and Mannheim in Germany.
class CalculateTime : IDisposable
{
MapPoint.Application app;
MapPoint.Map map;
MapPoint.Route route;
public CalculateTime()
{
app = (MapPoint.Application)Activator.CreateInstance(Type.GetTypeFromProgID("mappoint.application"));
map = app.ActiveMap;
route = map.ActiveRoute;
}
public class Place
{
public string City { get; set; }
public string Street { get; set; }
public string Postal { get; set; }
public Place(string City = "", string Street = "", string Postal = "")
{
this.City = City;
this.Street = Street;
this.Postal = Postal;
}
}
public class Place
{
public string City { get; set; }
public string Street { get; set; }
public string Postal { get; set; }
public Place(string City = "", string Street = "", string Postal = "")
{
this.City = City;
this.Street = Street;
this.Postal = Postal;
}
}
public TimeSpan Calculate(Place From, Place To)
{
Stopwatch sw = new Stopwatch();
MapPoint.FindResults frFromCollection = map.FindAddressResults(City: From.City, Street: From.Street, PostalCode: From.Postal);
MapPoint.FindResults frToCollection = map.FindAddressResults(City: To.City, Street: To.Street, PostalCode: To.Postal);
object frFrom = frFromCollection[1];
object frTo = frToCollection[1];
route.Waypoints.Add(frFrom);
route.Waypoints.Add(frTo);
sw.Start();
route.Calculate();
TimeSpan time = new TimeSpan(0, (int)(route.DrivingTime * 24 * 60), 0);
route.Clear();
sw.Stop();
Marshal.ReleaseComObject(frFromCollection);
Marshal.ReleaseComObject(frToCollection);
Marshal.ReleaseComObject(frFrom);
Marshal.ReleaseComObject(frTo);
MessageBox.Show(sw.Elapsed.Seconds + "." + sw.ElapsedMilliseconds);
return time;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~CalculateTime()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
map.Saved = true;
Marshal.ReleaseComObject(route);
Marshal.ReleaseComObject(map);
app.Quit();
Marshal.ReleaseComObject(app);
}
}
}
I remember there was another methode to calculate the route which shows a popup (not MapPoint.Route.Calculate()
, but I forgot it.
Does anybody have an idea how to speedup the calculation?
Greets
Wowa
EDIT:
I just tried this:
Adding the Waypoints via Code, but Calculate the ROute via MapPoint itself. this also takes extremly long. Somehow it seems the Waypoint are the problem, not the Calculate
methode
EDIT:
It looks like MapPoint already calculates the route in the background.
If i wait a few secondes before calling Calculate its finished within millisecondes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了每次后台处理应用程序外,请记住 COM 接口会增加一些开销。不多,但这是需要注意的事情。
我还注意到 MapPoint 2010 需要一两秒的时间来加载 - 比早期版本要长。
正如 Marc 指出的那样,隐藏大部分地图显示可导致大约 30% 的加速,因为 MapPoint 不必显示路线。
最后,路线计算根据航路点位置和道路密度/复杂性而有很大差异。计算一条路线可能需要 0.5 秒,但其他地方类似长度的路线则需要 2-3 秒。
最后,最后:-),MapPoint 的许多方面都针对人类用户而不是 API 使用进行了优化。例如,垃圾收集的优化绝对是面向人的,而不是面向批处理 API 的。 (MapPoint 会因大量使用批处理 API 而减慢速度,因为对于此类工作而言,垃圾收集器的调用频率不够高)
As well as spooling up the application each time, remember that the COM interface adds some overhead. Not much, but it is something to be aware of.
Also I've noticed that MapPoint 2010 takes a second or two to load - longer than earlier versions.
As Marc indicates, hiding much of the map display can result in roughly 30% speedups because MapPoint does not have to display the route.
Finally, route calculations vary a lot according to the waypoint locations, and road density/complexity. One route might take 0.5s to compute but a similar length route elsewhere takes 2-3 secs.
Finally, Finally :-) , many aspects of MapPoint are optimized for human users rather than API use. for example the garbage collection is definitely human-oriented, rather than batch API oriented in it's optimizations. (MapPoint will slow down with lots of batch API use as the garbage collector is not called often enough for this type of work)