多緒在Core2Duo上面效能降低問題
最近我寫一個單緒的 MEMCPY 卻發現他比在2個thread還快多
不知道為何
我程式如下
///////
*.cpp
///////
BOOL CMemcopy_TestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_bStop[0] = TRUE;
m_bStop[1] = TRUE;
return TRUE; // return TRUE unless you set the focus to a control
}
#include <afxmt.h>
#define WIDTH 720
#define HEIGHT 480
CCriticalSection g_CriticalSection[2];
DWORD CMemcopy_TestDlg::Video1Thread(LPVOID pv)
{
CMemcopy_TestDlg *pThis = (CMemcopy_TestDlg*)pv;
BYTE *ptsrc, *ptdst;
BYTE *bufin = new BYTE[ WIDTH*HEIGHT*3 ];
BYTE *bufout = new BYTE[ WIDTH*HEIGHT*3 ];
LARGE_INTEGER m_liPerfFreq, m_liPerfStart, liPerfNow;
QueryPerformanceFrequency(&m_liPerfFreq);
QueryPerformanceCounter(&m_liPerfStart);
DWORD nframe = 0;
char strMesg[256];
while( 1 )
{
ptsrc = bufin;
ptdst = bufout;
for( int j = 0; j < HEIGHT; j++ )
{
memcpy( ptdst, ptsrc, WIDTH*3 );
ptsrc += WIDTH*2;
ptdst += WIDTH*2;
}
nframe++;
if( nframe == 1000 )
{
QueryPerformanceCounter(&liPerfNow);
LONGLONG time = ( ((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000)/m_liPerfFreq.QuadPart);
sprintf( strMesg, "每秒 %d 次\n", (1000*1000)/time );
::OutputDebugString( strMesg );
QueryPerformanceCounter(&m_liPerfStart);
nframe = 0;
}
g_CriticalSection[ 0 ].Lock();
if( pThis->m_bStop[ 0 ] == TRUE )
{
g_CriticalSection[ 0 ].Unlock();
break;
}
g_CriticalSection[ 0 ].Unlock();
}
delete[] bufin;
delete[] bufout;
return 0;
}
DWORD CMemcopy_TestDlg::Video2Thread(LPVOID pv)
{
CMemcopy_TestDlg *pThis = (CMemcopy_TestDlg*)pv;
BYTE *ptsrc, *ptdst;
BYTE *bufin = new BYTE[ WIDTH*HEIGHT*3 ];
BYTE *bufout = new BYTE[ WIDTH*HEIGHT*3 ];
LARGE_INTEGER m_liPerfFreq, m_liPerfStart, liPerfNow;
QueryPerformanceFrequency(&m_liPerfFreq);
QueryPerformanceCounter(&m_liPerfStart);
DWORD nframe = 0;
char strMesg[256];
while( 1 )
{
ptsrc = bufin;
ptdst = bufout;
for( int j = 0; j < HEIGHT; j++ )
{
memcpy( ptdst, ptsrc, WIDTH*3 );
ptsrc += WIDTH*2;
ptdst += WIDTH*2;
}
nframe++;
if( nframe == 1000 )
{
QueryPerformanceCounter(&liPerfNow);
LONGLONG time = ( ((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000)/m_liPerfFreq.QuadPart);
sprintf( strMesg, "每秒 %d 次\n", (1000*1000)/time );
::OutputDebugString( strMesg );
QueryPerformanceCounter(&m_liPerfStart);
nframe = 0;
}
g_CriticalSection[ 1 ].Lock();
if( pThis->m_bStop[ 1 ] == TRUE )
{
g_CriticalSection[ 1 ].Unlock();
break;
}
g_CriticalSection[ 1 ].Unlock();
}
delete[] bufin;
delete[] bufout;
return 0;
}
void CMemcopy_TestDlg::OnOK()
{
if( m_bStop[0] == FALSE && m_bStop[1] == FALSE ) {
return;
}
m_bStop[ 0 ] = FALSE;
m_bStop[ 1 ] = FALSE;
DWORD nVideo1ThreadId;
DWORD nVideo2ThreadId;
HANDLE HndVideo1;
HANDLE HndVideo2;
HndVideo1 = CreateThread( NULL,
0,
(LPTHREAD_START_ROUTINE)InitialVideo1Thread,
this,
0,
&nVideo1ThreadId );
if( HndVideo1 == INVALID_HANDLE_VALUE )
{
AfxMessageBox("Create WriteThread failure!");
}
HndVideo2 = CreateThread( NULL,
0,
(LPTHREAD_START_ROUTINE)InitialVideo2Thread,
this,
0,
&nVideo2ThreadId );
if( HndVideo2 == INVALID_HANDLE_VALUE )
{
AfxMessageBox("Create WriteThread failure!");
}
}
void CMemcopy_TestDlg::OnCancel()
{
if( m_bStop[0]== FALSE && m_bStop[1]== FALSE ) {
return;
}
CDialog::OnCancel();
}
void CMemcopy_TestDlg::OnStop()
{
g_CriticalSection[ 0 ].Lock();
m_bStop[ 0 ] = TRUE;
g_CriticalSection[ 0 ].Unlock();
g_CriticalSection[ 1 ].Lock();
m_bStop[ 1 ] = TRUE;
g_CriticalSection[ 1 ].Unlock();
}
//////
*h
//////
class CMemcopy_TestDlg : public CDialog
{
// Construction
public:
CMemcopy_TestDlg(CWnd* pParent = NULL); // standard constructor
BOOL m_bStop[2];
DWORD Video1Thread(LPVOID pv);
static DWORD InitialVideo1Thread(LPVOID pv)
{
CMemcopy_TestDlg *pThis = (CMemcopy_TestDlg*)pv;
return pThis->Video1Thread(pThis);
}
DWORD Video2Thread(LPVOID pv);
static DWORD InitialVideo2Thread(LPVOID pv)
{
CMemcopy_TestDlg *pThis = (CMemcopy_TestDlg*)pv;
return pThis->Video2Thread(pThis);
}
// Dialog Data
//{{AFX_DATA(CMemcopy_TestDlg)
enum { IDD = IDD_MEMCOPY_TEST_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMemcopy_TestDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CMemcopy_TestDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
virtual void OnOK();
virtual void OnCancel();
afx_msg void OnStop();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};