c# - Any suggestions for optimizing this code? -
i have written code answer given here
my sample code follows
void process(int i) { input = (bitmap)bitmap.fromfile(@"filepath.bmp"); bitmap temp = new bitmap(input.width, input.height, pixelformat.format24bpprgb); graphics g = graphics.fromimage(temp); g.clear(color.red); g.drawimage(input, point.empty); temp.save(stream, imageformat.bmp); //i need stream thats further processing } void timer_ex() { int = 11; (; ; ) { if (i == 335) break; else { st.start(); process(i); st.stop(); console.writeline(st.elapsedmilliseconds.tostring()); //this time more time in thread sleeps st.reset(); thread.sleep(70); i++; } } }
so trying convert image rgb32 rgb24. takes more time on processing time in thread sleeps. sample code. me question "how can optimize process(int i) execute in 20 ms or less 100 ms?"
things dramatically improve code:-
your "input" image same image. load every time process() called. make member, , load once only.
your "temp" bitmap allocated every time call process(), again not need be. make member, , allocate once.
you don't dispose of of graphics objects. bitmaps should disposed when done them, should graphics.
not performance thing, loop very odd, , unreadable. why try reinvent language constructs built in? wrong with
for (int i=11; != 335 ; i++) { st.start(); process(i); st.stop(); console.writeline(st.elapsedmilliseconds.tostring()); //this time more time in thread sleeps st.reset(); thread.sleep(70); }
Comments
Post a Comment