Advertisement
Guest User

Untitled

a guest
May 16th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. // Boolean1.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. //Or演算って遅いのか?
  7. //And演算やXor演算の倍近くかかるんだけど。
  8. //やってる処理は10000×10000、32bit画像の全画素に固定の32bitの値をAnd演算、Or演算、Xor演算するだけ。
  9. //環境
  10. //Win7 64bit VS2008
  11. //計測結果※clock()を使用
  12. //&= 69
  13. //|= 140
  14. //^= 68
  15. //せめて80~100くらいに押さえたい。
  16.  
  17. #include <iostream>
  18. #include <Windows.h>
  19. #include <stdlib.h>
  20.  
  21. #define N 10000
  22. #define X 0x3456789a // magic number
  23. static int matrix[N][N];
  24.  
  25. struct foo {
  26.   __int64 start, end, freq;
  27.  
  28.   HANDLE hprocess;
  29.   DWORD oldclass;
  30.  
  31.   foo() : hprocess(GetCurrentProcess()),  oldclass(GetPriorityClass(hprocess)) {
  32.     Sleep(10);
  33.     SetPriorityClass(hprocess, REALTIME_PRIORITY_CLASS);
  34.     QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
  35.     QueryPerformanceCounter((LARGE_INTEGER*)&start);
  36.   }
  37.   ~foo() {
  38.     QueryPerformanceCounter((LARGE_INTEGER*)&end);
  39.     SetPriorityClass(hprocess, oldclass);
  40.     std::cout << (int)(end - start) << std::endl;
  41.   }
  42. };
  43.  
  44. int main()
  45. {
  46.   srand(0);
  47.   for (int i = 0; i < N; i++)
  48.       for (int j = 0; j < N; j++)
  49.       matrix[i][j] = rand() * RAND_MAX + rand();
  50.  
  51.   std::cout << "AND arithmetic" << std::endl;
  52.  
  53.   {
  54.     foo f;
  55.  
  56.     __asm {
  57.       mov esi, 0
  58. LABEL1:
  59.       mov edi, 0
  60. LABEL2:
  61.       imul ecx, esi, N
  62.       and dword ptr [ecx + edi + matrix], X
  63.       inc edi
  64.       cmp edi, N
  65.       jl LABEL2
  66.       inc esi
  67.       cmp esi, N
  68.       jl LABEL1
  69.     }
  70.   }
  71.  
  72.   srand(0);
  73.   for (int i = 0; i < N; i++)
  74.       for (int j = 0; j < N; j++)
  75.       matrix[i][j] = rand() * RAND_MAX + rand();
  76.  
  77.   std::cout << "OR arithmetic" << std::endl;
  78.  
  79.   {
  80.     foo f;
  81.  
  82.     __asm {
  83.       mov esi, 0
  84. LABEL3:
  85.       mov edi, 0
  86. LABEL4:
  87.       imul ecx, esi, N
  88.       or dword ptr [ecx + edi + matrix], X
  89.       inc edi
  90.       cmp edi, N
  91.       jl LABEL4
  92.       inc esi
  93.       cmp esi, N
  94.       jl LABEL3
  95.     }
  96.   }
  97.  
  98.   srand(0);
  99.   for (int i = 0; i < N; i++)
  100.       for (int j = 0; j < N; j++)
  101.       matrix[i][j] = rand() * RAND_MAX + rand();
  102.  
  103.   std::cout << "XOR arithmetic" << std::endl;
  104.  
  105.   {
  106.     foo f;
  107.  
  108.     __asm {
  109.       mov esi, 0
  110. LABEL5:
  111.       mov edi, 0
  112. LABEL6:
  113.       imul ecx, esi, N
  114.       xor dword ptr [ecx + edi + matrix], X
  115.       inc edi
  116.       cmp edi, N
  117.       jl LABEL6
  118.       inc esi
  119.       cmp esi, N
  120.       jl LABEL5
  121.     }
  122.   }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement