C
C#15mo 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 🌟15mo 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 🌟15mo ago
the ouput is this for making a image for desktop for each screen
Florian Voß
Florian Voß15mo ago
I'd try to run in parallel. Try to replace your foreach with Parallel.ForEach()
maria 🌟
maria 🌟15mo 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 🌟15mo ago
ok im tring to do it with graphics to draw the image directcly but doest ouput as espected
maria 🌟
maria 🌟15mo ago
Anton
Anton15mo ago
why not
Jester
Jester15mo 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 🌟15mo ago
make wallpaper
Jester
Jester15mo ago
oh ok. why with code though? instead of paint
maria 🌟
maria 🌟15mo 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
Jester15mo ago
oh okay cool
maria 🌟
maria 🌟15mo ago
and im making to support multiple wallpapers per screen its working but its very slow so im trying with graphics
maria 🌟
maria 🌟15mo ago
thanks all for the ideas
Anton
Anton15mo ago
I doubt this code is vectorized by the compiler, you may want to do simd too
Jester
Jester15mo 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 🌟15mo 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
Jester15mo ago
yeah, i dont think you could know otherwise
maria 🌟
maria 🌟15mo ago
wow
maria 🌟
maria 🌟15mo ago
both take 7 miliseconds before it would take like 9000 miliseconds 😳 well i suppose i dont need the parallel then how
Jester
Jester15mo ago
using a span and copyto
maria 🌟
maria 🌟15mo ago
instead of copyng pixel per pixel copy whole image?
ffmpeg -i me -f null -
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
Jester15mo 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 🌟15mo ago
but for me 7 milliseconds are already fast enoughbut for me 7 milliseconds are already fast enough
Accord
Accord15mo 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.