Hi all,
I have a script which renders a bitmap. On that bitmap I render a series of circles, each with “children” circles with the properties of each circle (position, radius, and a few other things) dependent upon the parent circle.
So as it is I have a List<Circle[]>, each Circle[] is composed of all the children of each circle in the array prior to it. Rendering the circles depends on the size, but generally even the larger ones only require about a second or two max to render. The smaller ones, several can be rendered per second.


Each circle is processed within a 3-fold nested for loop (i,j,k). The i loop refers to each Circle[], the j loop selects a Circle from the List with index i, then the k loop generates the Circles children in List index i+1.
However, with large numbers of circles, the overall rendering time becomes very large. I was hoping to learn to use multi-threading or asynchronous programming here, but I don’t even know if they are the right approach. How can I maybe speed this up?
Thanks! If it would help to see my code I can share that too.
EDIT: Simplified version followed by actual code
Actual code:
Yes, please share your code. I’m not sure what size your bitmap/circles are or how many you have to render, but individual large ones taking “a second or two” and “several” smaller circles “per second” sounds really suspect to me.
The base bitmap libraries are relatively slow, but not that slow.
Beyond that, maybe take a look at the WriteableBitmapExtensions library. It might help doing the bitmap painting. In theory, perhaps you could take their circle rendering code and adapt it to run multithreaded, but I’m suspect about the gains you might get. You might also have to do some tomfoolery with the threadsafety of the code/bitmap to implement it.
But again, I would suggest that you do try to create a minimal, reproducible example that we can copy/paste and run the code ourselves because the performance you’re reporting is pretty unexpected.
Sorry, I wasn’t that clear. I’m generating the circles beforehand, then going back and drawing them to the bitmap afterwards. The drawing process isn’t too bad, I’m trying to speed up the circle generation. After circle generation, the areas of the generated circles are added to or removed from a primary circle. My circles consist of List<Vector2> for perimeter and area respectively, and then I run a foreach loop on the primary circle for the area and set those vector2s (x,y) to a specific color on the bitmap.
I also added in my code.
Take a look at Parallel.ForEach and see if that helps you.
Use BenchmarkDotNet to actually verify it makes things quicker šŸ˜‰
Some special care might need to be done for gPos since you’re incrementing that in the child loop, either Interlocked.Increment or some other way of synchronising that state.


Use the Profiler in Visual Studio to see what areas of code are taking the longest.
Consider buffering everything in memory, then render to the bitmap only once when complete.
Also writing to the Console inside a tight loop can be very slow!
C# devs
null reference exceptions

source