Guest User

Untitled

a guest
Sep 25th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. internal unsafe class Program
  5. {
  6.     private const int COUNT = 500;
  7.     private const int WIDTH = 1920;
  8.     private const int HEIGHT = 1080;
  9.  
  10.     private static void Main()
  11.     {
  12.         var image = new int[WIDTH * HEIGHT];
  13.  
  14.         for (int i = 0; i < 10; i++)
  15.         {
  16.             var time = TestOriginal(image, WIDTH, HEIGHT);
  17.             Console.WriteLine("original: " + time);
  18.  
  19.             var time2 = Test(image, WIDTH, HEIGHT);
  20.             Console.WriteLine("optimized: " + time2);
  21.         }
  22.  
  23.         Console.ReadLine();
  24.     }
  25.  
  26.     private static long TestOriginal(int[] image, int width, int height)
  27.     {
  28.         var sw = Stopwatch.StartNew();
  29.         var buf = (int[])image.Clone();
  30.         for (int n = 0; n < COUNT; n++)
  31.         {
  32.             for (int y = 1; y < height - 1; y++)
  33.             {
  34.                 var p = width * y;
  35.                 for (int x = 1; x < width - 1; x++)
  36.                     if (image[p + x] != 0xffffff)
  37.                         buf[p + x] = (image[p - width + x] + image[p + x - 1] + image[p - width + x - 1] + image[p - width + x + 1] + image[p + width + x - 1] + image[p + width + x] + image[p + x + 1] + image[p + width + x + 1]) >> 3;
  38.             }
  39.             for (int y = 1; y < height - 1; y++)
  40.             {
  41.                 var p = width * y;
  42.                 for (int x = 1; x < width - 1; x++)
  43.                     if (buf[p + x] != 0xffffff)
  44.                         image[p + x] = (buf[p - width + x] + buf[p + x - 1] + buf[p - width + x - 1] + buf[p - width + x + 1] + buf[p + width + x - 1] + buf[p + width + x] + buf[p + x + 1] + buf[p + width + x + 1]) >> 3;
  45.             }
  46.         }
  47.         sw.Stop();
  48.         return sw.ElapsedMilliseconds;
  49.     }
  50.  
  51.     private static long Test(int[] image, int width, int height)
  52.     {
  53.         var stopwatch = Stopwatch.StartNew();
  54.  
  55.         var buffer = new int[image.Length];
  56.         for (int n = 0; n < COUNT; n++)
  57.         {
  58.             Blur(image, buffer, width, height);
  59.             Blur(buffer, image, width, height);
  60.         }
  61.  
  62.         stopwatch.Stop();
  63.         return stopwatch.ElapsedMilliseconds;
  64.     }
  65.  
  66.     private static void Blur(int[] input, int[] output, int width, int height)
  67.     {
  68.         fixed (int* inputPointer = input)
  69.         {
  70.             for (int y = 1; y < height - 1; y++)
  71.             {
  72.                 int index = y * width;
  73.                 int* currentLine = inputPointer + index + 1;
  74.                 int* prevLine = currentLine - width;
  75.                 int* nextLine = currentLine + width;
  76.  
  77.                 for (int x = 1; x < width - 1; x++)
  78.                 {
  79.                     if (*currentLine != 0xffffff)
  80.                     {
  81.                         int sum = *(prevLine - 1) + *prevLine + *(++prevLine) +
  82.                                   *(currentLine - 1) + *(++currentLine) +
  83.                                   *(nextLine - 1) + *nextLine + *(++nextLine);
  84.                         output[index + x] = sum >> 3;
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment