Advertisement
Guest User

Untitled

a guest
Jun 1st, 2015
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.14 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #ifdef WIN32
  4.  
  5. #include <windows.h>
  6. double get_time()
  7. {
  8.     LARGE_INTEGER t, f;
  9.     QueryPerformanceCounter(&t);
  10.     QueryPerformanceFrequency(&f);
  11.     return (double)t.QuadPart/(double)f.QuadPart;
  12. }
  13.  
  14. #else
  15.  
  16. #include <sys/time.h>
  17. #include <sys/resource.h>
  18.  
  19. double get_time()
  20. {
  21.     struct timeval t;
  22.     struct timezone tzp;
  23.     gettimeofday(&t, &tzp);
  24.     return t.tv_sec + t.tv_usec*1e-6;
  25. }
  26.  
  27. #endif
  28.  
  29. #define HEIGHT 10000
  30. #define WIDTH 10000
  31.  
  32. typedef int board[HEIGHT + 2][WIDTH + 2];
  33. typedef int board2[(HEIGHT + 2) * (WIDTH + 2)];
  34. typedef char board3[(HEIGHT + 2) * (WIDTH + 2)];
  35.  
  36. board b;
  37. board2 b2;
  38. board3 b3;
  39.  
  40. int min(int a, int b)
  41. {
  42.    return (a < b) ? a : b;
  43. }
  44.  
  45. int max(int a, int b)
  46. {
  47.    return (a > b) ? a : b;
  48. }
  49.  
  50. static int countNeighbors(board b, int x, int y)
  51. {
  52.    int n = 0;
  53.  
  54.    int x_left = max(0, x-1);
  55.    int x_right = min(HEIGHT, x+2);
  56.    int y_left = max(0, y-1);
  57.    int y_right = min(WIDTH, y+2);
  58.  
  59.    int xx, yy;
  60.    for (xx = x_left; xx < x_right; ++xx) {
  61.        for (yy = y_left; yy < y_right; ++yy) {
  62.            n += b[xx][yy];
  63.        }
  64.    }
  65.  
  66.    return n - b[x][y];
  67. }
  68.  
  69. int countNeighborsFast(board b, int x, int y)
  70. {
  71.     int n = 0;
  72.     n += b[x-1][y-1];
  73.     n += b[x][y-1];
  74.     n += b[x+1][y-1];
  75.     n += b[x-1][y];
  76.     n += b[x+1][y];
  77.     n += b[x-1][y+1];
  78.     n += b[x][y+1];
  79.     n += b[x+1][y+1];
  80.     return n;
  81. }
  82.  
  83. int countNeighborsLinear(board2 b, int x, int y)
  84. {
  85.     int n = 0;
  86.     int pos = (WIDTH + 2) * y + x;
  87.     n += b[pos - 1 - (WIDTH + 2)];
  88.     n += b[pos     - (WIDTH + 2)];
  89.     n += b[pos + 1 - (WIDTH + 2)];
  90.     n += b[pos - 1];
  91.     n += b[pos + 1];
  92.     n += b[pos - 1 + (WIDTH + 2)];
  93.     n += b[pos     + (WIDTH + 2)];
  94.     n += b[pos + 1 + (WIDTH + 2)];
  95.     return n;
  96. }
  97.  
  98. int countNeighborsLinearAndChars(board3 b, int x, int y)
  99. {
  100.     int n = 0;
  101.     int pos = (WIDTH + 2) * y + x;
  102.     n += b[pos - 1 - (WIDTH + 2)];
  103.     n += b[pos     - (WIDTH + 2)];
  104.     n += b[pos + 1 - (WIDTH + 2)];
  105.     n += b[pos - 1];
  106.     n += b[pos + 1];
  107.     n += b[pos - 1 + (WIDTH + 2)];
  108.     n += b[pos     + (WIDTH + 2)];
  109.     n += b[pos + 1 + (WIDTH + 2)];
  110.     return n;
  111. }
  112.  
  113. int countNeighborsLinearAndCharsAndLinearLoop(board3 b, int pos)
  114. {
  115.     int n = 0;
  116.     n += b[pos - 1 - (WIDTH + 2)];
  117.     n += b[pos     - (WIDTH + 2)];
  118.     n += b[pos + 1 - (WIDTH + 2)];
  119.     n += b[pos - 1];
  120.     n += b[pos + 1];
  121.     n += b[pos - 1 + (WIDTH + 2)];
  122.     n += b[pos     + (WIDTH + 2)];
  123.     n += b[pos + 1 + (WIDTH + 2)];
  124.     return n;
  125. }
  126.  
  127. int countNeighborsLinearAndCharsAndLinearLoopAndSum(board3 b, int pos)
  128. {
  129.     return
  130.     b[pos - 1 - (WIDTH + 2)] +
  131.     b[pos     - (WIDTH + 2)] +
  132.     b[pos + 1 - (WIDTH + 2)] +
  133.     b[pos - 1] +
  134.     b[pos + 1] +
  135.     b[pos - 1 + (WIDTH + 2)] +
  136.     b[pos     + (WIDTH + 2)] +
  137.     b[pos + 1 + (WIDTH + 2)];
  138. }
  139.  
  140. int main()
  141. {
  142.    int x, y;
  143.    double start = get_time();
  144.    int sum = 0;
  145.  
  146.    for (y = 0; y < HEIGHT; y++)
  147.       for (x = 0; x < WIDTH; x++)
  148.          sum += countNeighbors(b, x, y);
  149.  
  150.    printf("Original: %.2fs\n", get_time() - start);
  151.  
  152.    start = get_time();
  153.  
  154.    for (y = 1; y <= HEIGHT; y++)
  155.       for (x = 1; x <= WIDTH; x++)
  156.      sum += countNeighborsFast(b, x, y);
  157.  
  158.    printf("Mine: %.2fs\n", get_time() - start);
  159.  
  160.    start = get_time();
  161.  
  162.    for (y = 1; y <= HEIGHT; y++)
  163.       for (x = 1; x <= WIDTH; x++)
  164.      sum += countNeighborsLinear(b2, x, y);
  165.  
  166.    printf("Linear: %.2fs\n", get_time() - start);
  167.  
  168.    start = get_time();
  169.  
  170.    for (y = 1; y <= HEIGHT; y++)
  171.       for (x = 1; x <= WIDTH; x++)
  172.      sum += countNeighborsLinearAndChars(b3, x, y);
  173.  
  174.    printf("LinearAndChars: %.2fs\n", get_time() - start);
  175.  
  176.    start = get_time();
  177.  
  178.    int i;
  179.    for (i = WIDTH + 3; i <= (WIDTH + 2) * (HEIGHT + 1) - 2; i++)
  180.      sum += countNeighborsLinearAndCharsAndLinearLoop(b3, i);
  181.  
  182.    printf("LinearAndCharsAndLinearLoop: %.2fs\n", get_time() - start);
  183.  
  184.    start = get_time();
  185.  
  186.    for (i = WIDTH + 3; i <= (WIDTH + 2) * (HEIGHT + 1) - 2; i++)
  187.      sum += countNeighborsLinearAndCharsAndLinearLoopAndSum(b3, i);
  188.  
  189.    printf("LinearAndCharsAndLinearLoopAndSum: %.2fs\n", get_time() - start);
  190.  
  191.    return sum;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement