C
C#2y ago
maria 🌟

❔ How i can write this code more efficient

i need advice about this code if it can be faster
27 Replies
maria 🌟
maria 🌟2y ago
private static Image GenerateScreensImage(Image[] Images)
{
var Result = ResolveScreen();

Console.WriteLine("Highest X:" + Result.XHighest + " Highest Y:" + Result.YHighest);

Bitmap Image = new Bitmap(Result.XHighest, Result.YHighest);

Console.WriteLine("Gemerating image");
foreach (var Element in Result.ScreensInfo)
{
Console.WriteLine("Making image " + Element.Index);
Console.WriteLine("Screen:" + Element.Index + " X:" + Element.XPoint + " Y:" + Element.YPoint);

for (int x = Element.WidthPoint; x < Element.XPoint; x++)
{
for (int y = Element.HeightPoint; y < Element.YPoint; y++)
{
Color PixelColor = ((Bitmap)Images[Element.Index]).GetPixel(x - Element.WidthPoint, y - Element.HeightPoint);

Image.SetPixel(x, y, PixelColor);
}
}
}

return Image;
}
private static Image GenerateScreensImage(Image[] Images)
{
var Result = ResolveScreen();

Console.WriteLine("Highest X:" + Result.XHighest + " Highest Y:" + Result.YHighest);

Bitmap Image = new Bitmap(Result.XHighest, Result.YHighest);

Console.WriteLine("Gemerating image");
foreach (var Element in Result.ScreensInfo)
{
Console.WriteLine("Making image " + Element.Index);
Console.WriteLine("Screen:" + Element.Index + " X:" + Element.XPoint + " Y:" + Element.YPoint);

for (int x = Element.WidthPoint; x < Element.XPoint; x++)
{
for (int y = Element.HeightPoint; y < Element.YPoint; y++)
{
Color PixelColor = ((Bitmap)Images[Element.Index]).GetPixel(x - Element.WidthPoint, y - Element.HeightPoint);

Image.SetPixel(x, y, PixelColor);
}
}
}

return Image;
}
maria 🌟
maria 🌟2y ago
the ouput is this for making a image for desktop for each screen
Florian Voß
Florian Voß2y ago
I'd try to run in parallel. Try to replace your foreach with Parallel.ForEach()
maria 🌟
maria 🌟2y ago
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
maria 🌟
maria 🌟2y ago
ok im tring to do it with graphics to draw the image directcly but doest ouput as espected
maria 🌟
maria 🌟2y ago
Anton
Anton2y ago
why not
Jester
Jester2y ago
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?
maria 🌟
maria 🌟2y ago
make wallpaper
Jester
Jester2y ago
oh ok. why with code though? instead of paint
maria 🌟
maria 🌟2y ago
the program gets random images related of the word thath you give and it changes the background every x time you selected automatcly
Jester
Jester2y ago
oh okay cool
maria 🌟
maria 🌟2y ago
and im making to support multiple wallpapers per screen its working but its very slow so im trying with graphics
maria 🌟
maria 🌟2y ago
thanks all for the ideas
Anton
Anton2y ago
I doubt this code is vectorized by the compiler, you may want to do simd too
Jester
Jester2y ago
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
maria 🌟
maria 🌟2y ago
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
Jester
Jester2y ago
yeah, i dont think you could know otherwise
maria 🌟
maria 🌟2y ago
wow
maria 🌟
maria 🌟2y ago
both take 7 miliseconds before it would take like 9000 miliseconds 😳 well i suppose i dont need the parallel then how
Jester
Jester2y ago
using a span and copyto
maria 🌟
maria 🌟2y ago
instead of copyng pixel per pixel copy whole image?
Omnissiah
Omnissiah2y ago
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
Jester
Jester2y ago
span with the copyto is more optimized than a simole for loop yeah everything in the gpu would be even faster ofc
maria 🌟
maria 🌟2y ago
but for me 7 milliseconds are already fast enoughbut for me 7 milliseconds are already fast enough
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server