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:-

  1. your "input" image same image. load every time process() called. make member, , load once only.

  2. your "temp" bitmap allocated every time call process(), again not need be. make member, , allocate once.

  3. 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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -