Wednesday, January 24, 2007

Unsafe ใน C#: ความเร็วแตกต่างกันอย่างสิ้นเชิง

เขียนโปรแกรมแปลงภาพสีเป็น GrayScale เขียนโค้ดแบบ unsafe ดังนี้

byte* p = (byte*)(void*)Scan0;
for (int y = 0; y < Image.height; y++, p+= nOffset, h += gOffset)
{
for (int x = 0; x < Image.Width; x++, p+=3, h++)
{
*h = (byte)(0.299 * p[2] + 0.587 * p[1] + 0.114 * p[0]);
}
}


ใช้เวลารัน นาน แบบเห็นได้ชัดเจน แต่พอเปลี่ยนโค้ดเป็นแบบนี้

byte* p = (byte*)(void*)Scan0;
int height = Image.height;
int width = Image.width;
for (int y = 0; y < height; y++, p+= nOffset, h += gOffset)
{
for (int x = 0; x < width; x++, p+=3, h++)
{
*h = (byte)(0.299 * p[2] + 0.587 * p[1] + 0.114 * p[0]);
}
}

ปรากฏว่าเห็นความแตกต่างเรื่องความเร็วได้อย่างชัดเจน (อันหลังเร็วกว่าอันแรกอย่างแรง)
- -"

2 comments:

  1. [B]NZBsRus.com[/B]
    Escape Idle Downloads Using NZB Downloads You Can Easily Find Movies, Console Games, Music, Software & Download Them @ Accelerated Rates

    [URL=http://www.nzbsrus.com][B]Usenet Search[/B][/URL]

    ReplyDelete
  2. Subsidize to pass the unreasoning with two backs casinos? scrutinization this k [url=http://www.realcazinoz.com]casino[/url] advisor and supplied with up online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
    you can also into our untrained [url=http://freecasinogames2010.webs.com]casino[/url] orientate at http://freecasinogames2010.webs.com and pull down enfold of loyal compressed bread !
    another latest [url=http://www.ttittancasino.com]casino spiele[/url] locality is www.ttittancasino.com , pro german gamblers, petiole in manumitted online casino bonus.

    ReplyDelete