Select to view content in your preferred language

Improve performance with AddRange()

590
0
10-09-2019 09:54 AM
Labels (1)
MichaelBranscomb
Esri Frequent Contributor
2 0 590

In ArcGIS Runtime SDK for .NET 100.6 we added a useful API enhancement that can improve the performance of your apps with the new Esri.ArcGISRuntime.RuntimeCollection.AddRange method. There are several classes in the API that derive from the base RuntimeCollection, making this method available on many commonly used types such as GraphicCollection, GraphicsOverlayCollection, LayerCollection, UniqueValueCollection, and ClassBreakCollection.

 

The AddRange method helps improve performance for your apps by batching internal ArcGIS Runtime logic related to error handling and collection changed events. Internal benchmark tests run by the ArcGIS Runtime SDK for .NET development team using BenchmarkDotNet to compare individual GraphicCollection.Add calls with the GraphicCollection.AddRange call showed a 10-15% reduction in execution time when adding large numbers of Graphic instances to a GraphicCollection (i.e. GraphicsOverlay.Graphics). Additionally there was a 85-90% reduction in temporary allocations which reduces the memory consumed and reduces the workload for garbage collection. 

 

The two code snippets below show an example using the AddRange method, new at 100.6, and an example using the existing Add method.

 

AddRange method

GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
graphicsOverlay.Renderer =
 new SimpleRenderer(new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, 
  Color.CornflowerBlue, 10));
SpatialReference spatialReference = SpatialReferences.WebMercator;
Random random = new Random();
List<Graphic> graphicList = new List<Graphic>(10000);
for (int i = 0; i < 10000; i++)
{
 Graphic graphic =
 new Graphic(new MapPoint(random.Next(0, 20000000), random.Next(0, 20000000), 
  spatialReference));
 graphicList.Add(graphic);
}
graphicsOverlay.Graphics.AddRange(graphicList);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

Add method

GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
graphicsOverlay.Renderer =
 new SimpleRenderer(new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, 
  Color.CornflowerBlue, 10));
SpatialReference spatialReference = SpatialReferences.WebMercator;
Random random = new Random();
for (int i = 0; i < 10000; i++)
{
 Graphic graphic =
 new Graphic(new MapPoint(random.Next(0, 20000000), random.Next(0, 20000000), 
  spatialReference));
 graphicsOverlay.Graphics.Add(graphic);
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍