❔ How i can write this code more efficient
i need advice about this code if it can be faster
27 Replies
the ouput is this for making a image for desktop for each screen
I'd try to run in parallel. Try to replace your foreach with Parallel.ForEach()
you cant acess a bit map twice from 2 threads at same time
i think the best is to lock bits and modify the raw data of the bytes
ok im tring to do it with graphics to draw the image directcly but doest ouput as espected
why not
Afaik GetPixel / SetPixel is not that fast compared to using pointers
https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.lockbits?view=dotnet-plat-ext-7.0#system-drawing-bitmap-lockbits(system-drawing-rectangle-system-drawing-imaging-imagelockmode-system-drawing-imaging-pixelformat)
https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imaging.bitmapdata?view=dotnet-plat-ext-7.0
yeah getpixel and setpixel are realy slow but everything in System.Drawing is really slow actually
@Gato what are you trying to do? take a screenshot or make a wallpaper?
are you sure you read the parameters of drawimage correctly?
make wallpaper
oh ok. why with code though? instead of paint
the program gets random images related of the word thath you give and it changes the background every x time you selected automatcly
oh okay
cool
and im making to support multiple wallpapers per screen
its working but its very slow so im trying with graphics
thanks all for the ideas
I doubt this code is vectorized by the compiler, you may want to do simd too
you could probably still improve the speed of the copying in that loop but other than that, looks alright
are you sure you parallel in this case is faster than not using it? i think it depends on if it takes longer to create a thread than to copy the image
im making a thread for each whole image so idk
i suppose im gona try it one with and one without and check the times with a stopwatch
yeah, i dont think you could know otherwise
wow
both take 7 miliseconds
before it would take like 9000 miliseconds 😳
well i suppose i dont need the parallel then
how
using a span and copyto
instead of copyng pixel per pixel copy whole image?
the fast way would obviously be using directx (or opengl)
but still, working a little lower with a true hdc and memory (and bitblt) would be reasonably faster
span with the copyto is more optimized than a simole for loop
yeah everything in the gpu would be even faster ofc
but for me 7 milliseconds are already fast enoughbut for me 7 milliseconds are already fast enough
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.