home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_07 / PixelSpeed / PixelSpeed.cpp next >
Encoding:
C/C++ Source or Header  |  2000-05-12  |  4.3 KB  |  177 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : pixelspeed.cpp                                                         //
  10. //  Description: GDI pixel drawing speed test                                        //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <tchar.h>
  19.  
  20. #include "..\..\include\timer.h"
  21. #include "..\..\include\Dialog.h"
  22. #include "..\..\include\ListView.h"
  23.  
  24. #include "resource.h"
  25.  
  26. class KQuickTimer : public KTimer
  27. {
  28. public:
  29.     unsigned cpuspeed10;
  30.  
  31.     KQuickTimer()
  32.     {
  33.         Start(); 
  34.         Sleep(1000); 
  35.     
  36.         cpuspeed10 = (unsigned)(Stop()/100000);
  37.     }
  38.  
  39.     void Query(TCHAR speed[], TCHAR overhead[])
  40.     {
  41.         wsprintf(speed, "%d.%d Mhz", cpuspeed10 / 10,  cpuspeed10 % 10);
  42.         wsprintf(overhead, "%d ns", (unsigned) m_overhead);
  43.     }
  44. };
  45.  
  46. class KSpeedTest : public KDialog
  47. {
  48.     KListView   report;
  49.     KQuickTimer timer;
  50.     unsigned    measurement[5];
  51.  
  52.     void Measure(void);
  53.  
  54.     void Report(unsigned time, TCHAR * pTest)
  55.     {
  56.         report.AddItem(0, pTest);
  57.         report.AddItem(1, time);        // clock cycle count
  58.  
  59.         time = time * 10000 / timer.cpuspeed10; // ns
  60.         report.AddItem(2, time);
  61.  
  62.         if ( time )
  63.             report.AddItem(3, 1000 * 1000 * 1000 / time); // per-second
  64.     }
  65.  
  66.     virtual BOOL DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  67.     {
  68.         switch (uMsg)
  69.         {
  70.             case WM_INITDIALOG:
  71.                 m_hWnd = hWnd;
  72.  
  73.                 report.FromDlgItem(hWnd, IDC_REPORT);
  74.  
  75.                 report.AddColumn(0, 120, "Test");
  76.                 report.AddColumn(1,  80, "clock");
  77.                 report.AddColumn(2,  80, "nanosec");
  78.                 report.AddColumn(3,  80, "per-sec");
  79.                 
  80.                 {
  81.                     TCHAR speed[32], overhead[32];
  82.                     timer.Query(speed, overhead);
  83.                     SetDlgItemText(hWnd, IDC_CPU,      speed);
  84.                     SetDlgItemText(hWnd, IDC_OVERHEAD, overhead);
  85.                 }
  86.  
  87.                 Measure();
  88.                 Report(measurement[0]/10000, "SetPixel(RGB)");
  89.                 Report(measurement[1]/10000, "SetPixel(PALETTERGB)");
  90.                 Report(measurement[2]/10000, "SetPixel(PALETTEINDEX)");
  91.                 Report(measurement[3]/10000, "SetPixelV(RGB)");
  92.                 Report(measurement[4]/10000, "GetPixel()");
  93.     
  94.                 return TRUE;
  95.  
  96.             case WM_COMMAND:
  97.                 switch ( LOWORD(wParam) )
  98.                 {
  99.                     case IDOK:
  100.                         EndDialog(hWnd, 1);
  101.                         return TRUE;
  102.  
  103.                     case IDC_TU:
  104.                         ;
  105.                 }
  106.         }
  107.  
  108.         return FALSE;
  109.     }
  110.  
  111. };
  112.  
  113.  
  114. void KSpeedTest::Measure(void)
  115. {
  116.     HDC hDC = GetDC(NULL);
  117.  
  118.     HPALETTE hPalette = CreateHalftonePalette(hDC);
  119.     HPALETTE hOld     = SelectPalette(hDC, hPalette, FALSE);
  120.     RealizePalette(hDC);
  121.  
  122.     for (int test=0; test<5; test++)
  123.     {
  124.         int i, j;
  125.  
  126.         timer.Start(); 
  127.     
  128.         switch ( test )
  129.         {
  130.             case 0:
  131.                 for (i=0; i<100; i++)
  132.                 for (j=0; j<100; j++)
  133.                     SetPixel(hDC, i, j, RGB(i, j, i+i));
  134.                 break;
  135.  
  136.             case 1:
  137.                 for (i=0; i<100; i++)
  138.                 for (j=0; j<100; j++)
  139.                     SetPixel(hDC, i, j, PALETTERGB(i, j, i+i));
  140.                 break;
  141.  
  142.             case 2:
  143.                 for (i=0; i<100; i++)
  144.                 for (j=0; j<100; j++)
  145.                     SetPixel(hDC, i, j, PALETTEINDEX(i));
  146.                 break;
  147.  
  148.             case 3:
  149.                 for (i=0; i<100; i++)
  150.                 for (j=0; j<100; j++)
  151.                     SetPixelV(hDC, i, j, RGB(i, j, i+i));
  152.                 break;
  153.  
  154.             case 4:
  155.                 for (i=0; i<100; i++)
  156.                 for (j=0; j<100; j++)
  157.                     GetPixel(hDC, i, j);
  158.                 break;
  159.         }
  160.  
  161.         measurement[test] = (unsigned) timer.Stop();
  162.     }
  163.  
  164.     SelectObject(hDC, hOld);
  165.     DeleteObject(hPalette);
  166.  
  167.     ReleaseDC(NULL, hDC);
  168. }
  169.  
  170.  
  171. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
  172. {
  173.     KSpeedTest speed;
  174.  
  175.     return speed.Dialogbox(hInstance, MAKEINTRESOURCE(IDD_SPEED));
  176. }
  177.