Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- internal unsafe class Program
- {
- private const int COUNT = 500;
- private const int WIDTH = 1920;
- private const int HEIGHT = 1080;
- private static void Main()
- {
- var image = new int[WIDTH * HEIGHT];
- for (int i = 0; i < 10; i++)
- {
- var time = TestOriginal(image, WIDTH, HEIGHT);
- Console.WriteLine("original: " + time);
- var time2 = Test(image, WIDTH, HEIGHT);
- Console.WriteLine("optimized: " + time2);
- }
- Console.ReadLine();
- }
- private static long TestOriginal(int[] image, int width, int height)
- {
- var sw = Stopwatch.StartNew();
- var buf = (int[])image.Clone();
- for (int n = 0; n < COUNT; n++)
- {
- for (int y = 1; y < height - 1; y++)
- {
- var p = width * y;
- for (int x = 1; x < width - 1; x++)
- if (image[p + x] != 0xffffff)
- 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;
- }
- for (int y = 1; y < height - 1; y++)
- {
- var p = width * y;
- for (int x = 1; x < width - 1; x++)
- if (buf[p + x] != 0xffffff)
- 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;
- }
- }
- sw.Stop();
- return sw.ElapsedMilliseconds;
- }
- private static long Test(int[] image, int width, int height)
- {
- var stopwatch = Stopwatch.StartNew();
- var buffer = new int[image.Length];
- for (int n = 0; n < COUNT; n++)
- {
- Blur(image, buffer, width, height);
- Blur(buffer, image, width, height);
- }
- stopwatch.Stop();
- return stopwatch.ElapsedMilliseconds;
- }
- private static void Blur(int[] input, int[] output, int width, int height)
- {
- fixed (int* inputPointer = input)
- {
- for (int y = 1; y < height - 1; y++)
- {
- int index = y * width;
- int* currentLine = inputPointer + index + 1;
- int* prevLine = currentLine - width;
- int* nextLine = currentLine + width;
- for (int x = 1; x < width - 1; x++)
- {
- if (*currentLine != 0xffffff)
- {
- int sum = *(prevLine - 1) + *prevLine + *(++prevLine) +
- *(currentLine - 1) + *(++currentLine) +
- *(nextLine - 1) + *nextLine + *(++nextLine);
- output[index + x] = sum >> 3;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment