DotNet Bitmap Creation Optimization
Dot Net Bitmap Creation
In one of my side project, I am playing with fractals algotrithms to generage images. I am using with this project to explore Async/Await use and parrallelism. The ultimate goal is to use GPU ressources to offload some of the compute extensive tasks. While playing with generation of 500x500 images and 2.000x2.000 images, I realized that the code to load the result into a bitmap was way more expensive than the computation of the underlying matrix. Here is how I optimized the Bitmap creation
First try
My algorithm generates a grid of double. I am then using the double to select a color in an array.
|
|
Optimized version
This requires the activation of the Unsage compilation in the properties of the project.
Parallel.For
is used to exploit the parrallelism.
The complexity is to process the grid as an array of values. This is easing using the modulo (%
) and divide (/
) operators.
Code is documented for a better lecture.
|
|
Results
To have a better appreciation, I took the measures 3 times for each tests. The first tests results in 1x1K are not too bad. But the result for 4x4K at the bottom are showing that the improved version is 16 times faster ! And staying below 0.5 s !
Algo | Size | Time Gen | Time BPM |
---|---|---|---|
Not Opti | 1kx1k | 18ms 154 | 262ms 218 |
Not Opti | 1kx1k | 13ms 925 | 259ms 556 |
Not Opti | 1kx1k | 15ms 192 | 262ms 743 |
Opti | 1kx1k | 14ms 181 | 59ms 246 |
Opti | 1kx1k | 14ms 781 | 61ms 056 |
Opti | 1kx1k | 14ms 015 | 38ms 031 |
Not Opti | 2kx2k | 61ms 571 | 1s 49ms 074 |
Not Opti | 2kx2k | 63ms 288 | 1s 66ms 635 |
Not Opti | 2kx2k | 62ms 419 | 1s 56ms 018 |
Opti | 2kx2k | 61ms 119 | 87ms 442 |
Opti | 2kx2k | 64ms 187 | 65ms 165 |
Opti | 2kx2k | 62ms 798 | 99ms 169 |
Not Opti | 4kx4k | 237ms 660 | 4s 348ms 471 |
Not Opti | 4kx4k | 237ms 255 | 4s 244ms 536 |
Not Opti | 4kx4k | 237ms 441 | 4s 343ms 525 |
Opti | 4kx4k | 183ms 192 | 255ms 756 |
Opti | 4kx4k | 247ms 003 | 249ms 913 |
Opti | 4kx4k | 231ms 056 | 170ms 567 |