Click here to Skip to main content
14,605,816 members

ATL / WTL / STL

 
GeneralRe: create a window for callbacks Pin
bkelly1311-Nov-13 4:49
Memberbkelly1311-Nov-13 4:49 
GeneralRe: create a window for callbacks Pin
Richard MacCutchan11-Nov-13 6:33
mveRichard MacCutchan11-Nov-13 6:33 
GeneralRe: create a window for callbacks Pin
bkelly1311-Nov-13 8:33
Memberbkelly1311-Nov-13 8:33 
GeneralRe: create a window for callbacks Pin
Richard MacCutchan11-Nov-13 22:37
mveRichard MacCutchan11-Nov-13 22:37 
GeneralRe: create a window for callbacks Pin
bkelly1312-Nov-13 15:50
Memberbkelly1312-Nov-13 15:50 
AnswerRe: What is the window argument in WSAAsyncSelect(...) Pin
Orjan Westin19-Nov-13 23:25
professionalOrjan Westin19-Nov-13 23:25 
GeneralRe: What is the window argument in WSAAsyncSelect(...) Pin
bkelly1321-Nov-13 14:51
Memberbkelly1321-Nov-13 14:51 
QuestionMFC vs STL performance test Pin
Alexander Fedorov18-Oct-13 11:42
MemberAlexander Fedorov18-Oct-13 11:42 
Hi guys,
I did some performance comparison between MFC and STL containers and I think maybe it could evolve into an article here at Codeproject. Please have a look at my code and suggest some improvements to make comparison more valid. So far I found that STL still sucks big time even after 10 or 15 years of polishing (and neglecting of MFC) in VS2012. Following is complete source code, just drop it into console project with MFC support, compile and run in Release mode.

// MFCvsSTL.cpp : Performance comparison between MFC and STL containers
// (c) Alex Fedorov http:://alexf.name 2013

#include "stdafx.h"
#include "MFCvsSTL.h" // nothing special here

#include <unordered_map>
#include <map>
#include <vector>
#include <list>


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// The one and only application object

CWinApp theApp;

using namespace std;

// typedef map<DWORD, void*> stlmap;
typedef unordered_map<DWORD, void*> stlmap;

int stlMap(int nCount)
{
	int nSize = (int)(1.2 * (float)nCount);
	stlmap a(nSize);
	// unordered_map <DWORD, void*> a(nSize);
	for( int i = 0; i < nCount; ++i )
	{
		a[ i ] = (void *) i;
	}
	stlmap::iterator iter;
	for( int j = 0; j < nCount; ++j )
	{
		iter = a.find( ( abs( rand() * j ) % nCount ) ); 
	}
	return 1;
}

int mfcMap(int nCount)
{
	int nSize = (int)(1.2 * (float)nCount);
	// CMapWordToPtr a;
	CMap<DWORD, DWORD, void*, void*> a;
	a.InitHashTable( nSize );
	for( int i = 0; i < nCount; ++i )
	{
		a[ i ] = (void *) i;
	}
	void * val;
	for( int j = 0; j < nCount; ++j )
	{
		a.Lookup( ( abs( rand() * j ) % nCount ), val );
	}
	return 0;
}

int stlArray(int nCount)
{
	vector <int> bigarray;
	int nMs = bigarray.max_size();
	try
	{
		bigarray.reserve(nCount);
	}
	catch (...)
	{
		CString str;
		str.Format(_T("Memory allocation error trying to reserve %d elements. vector.max_size=%d\r\n"), nCount, nMs);
		_tprintf(str);
		return 0;
	}
	for(int k = 0; k < nCount; ++k)
	{
		bigarray.push_back(k);
		// bigarray[k] = k;
	}
	int ret = bigarray.size();
	return ret;
}

int mfcArray(int nCount)
{
	// CArray<int,int> arr;
	// OCArray<int> arr;
	CUIntArray arr;
	arr.SetSize(0, nCount);
	for(int k = 0; k < nCount; ++k)
	{
		arr.Add(k);
		// arr[k] = k;
	}
	int ret = arr.GetCount();
	return ret;
}

int mfcList(int nCount)
{
	CList<int, int> a;
	for(int k = 0; k < nCount; ++k)
	{
		a.AddHead(k);
	}
	for( int j = 0; j < nCount; ++j )
	{
		int n = abs(rand() * j ) % nCount;
		POSITION pos = a.Find(n);
	}
	return 1;
}

int stlList(int nCount)
{
	list<int> mylist;
	for(int k = 0; k < nCount; ++k)
	{
		mylist.push_front (k);
	}
	for( int j = 0; j < nCount; ++j )
	{
		int n = abs(rand() * j ) % nCount;
		std::list<int>::iterator findIter = std::find(mylist.begin(), mylist.end(), n);
	}
	return 1;
}

class CTimeHelper2013
{
public:
	SYSTEMTIME m_st1;
	SYSTEMTIME m_st2;
	BOOL m_bStarted;
	//
	int Start();
	int DiffMS();
	//
	CTimeHelper2013();
	virtual ~CTimeHelper2013();
};

int timeMFCl(int nCount)
{
	CTimeHelper2013 th;
	th.Start();
	mfcList(nCount);
	return th.DiffMS();
}

int timeSTLl(int nCount)
{
	CTimeHelper2013 th;
	th.Start();
	stlList(nCount);
	return th.DiffMS();
}

int timeMFCv(int nCount)
{
	CTimeHelper2013 th;
	th.Start();
	mfcArray(nCount);
	return th.DiffMS();
}

int timeSTLv(int nCount)
{
	CTimeHelper2013 th;
	th.Start();
	stlArray(nCount);
	return th.DiffMS();
}

int timeMFCh(int nCount)
{
	CTimeHelper2013 th;
	th.Start();
	mfcMap(nCount);
	return th.DiffMS();
}

int timeSTLh(int nCount)
{
	CTimeHelper2013 th;
	th.Start();
	stlMap(nCount);
	return th.DiffMS();
}

// http://forums.codeguru.com/showthread.php?129321-STL-vs-MFC
int testHash(int nCount)
{
	int d2 = timeSTLh(nCount);
	int d1 = timeMFCh(nCount);
	CString str;
	str.Format(_T("CMap vs unordered_map. Fill & find %d elements. MFC (%d)ms STL (%d)ms\r\n"), nCount, d1, d2);
	_tprintf(str);
	return 1;
}

int testArray(int nCount)
{
	int d2 = timeSTLv(nCount);
	int d1 = timeMFCv(nCount);
	CString str;
	str.Format(_T("CArray vs vector. Fill %d elements. MFC (%d)ms STL (%d)ms\r\n"), nCount, d1, d2);
	_tprintf(str);
	return 1;
}

int testList(int nCount)
{
	int d2 = timeSTLl(nCount);
	int d1 = timeMFCl(nCount);
	CString str;
	str.Format(_T("CList vs list. Fill & find %d elements. MFC (%d)ms STL (%d)ms\r\n"), nCount, d1, d2);
	_tprintf(str);
	return 1;
}

// http://blogs.msdn.com/b/vcblog/archive/2009/06/23/stl-performance.aspx
void perf_startup()
{
	SetThreadAffinityMask(GetCurrentThread(), 1);
	SetThreadIdealProcessor(GetCurrentThread(), 0);
	Sleep(1);
}

CTimeHelper2013::CTimeHelper2013()
{
	m_bStarted = 0;

}

CTimeHelper2013::~CTimeHelper2013()
{

}

int CTimeHelper2013::Start()
{
	GetLocalTime(&m_st1);
	m_bStarted = 1;
	return 1;
}

int CTimeHelper2013::DiffMS()
{
	GetLocalTime(&m_st2);
	m_bStarted = 0;
	int nRet1 = m_st2.wMilliseconds - m_st1.wMilliseconds;
	int nRet2 = m_st2.wSecond - m_st1.wSecond;
	int nRet3 = m_st2.wMinute - m_st1.wMinute;
	int nRet4 = m_st2.wHour - m_st1.wHour;
	int nRet5 = m_st2.wDay - m_st1.wDay;
	int nRet6 = m_st2.wMonth - m_st1.wMonth;
	int nRet7 = m_st2.wYear - m_st1.wYear;
	int nRet = 0;
	nRet += nRet1;
	nRet += nRet2 * 1000;
	nRet += nRet3 * 1000 * 60;
	nRet += nRet4 * 1000 * 60 * 60;
	nRet += nRet5 * 1000 * 60 * 60 * 24;
	nRet += nRet6 * 1000 * 60 * 60 * 24 * 30; // approximation
	nRet += nRet7 * 1000 * 60 * 60 * 24 * 365; // approximation
	return nRet;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
	HMODULE hModule = ::GetModuleHandle(NULL);

	if (NULL == hModule != NULL)
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
		return 1;
	}
	if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		return 2;
	}
	_tprintf(_T("MFC vs STL performance test\n"));
	perf_startup(); // unnecessary
	testList(1000 * 10);
	testList(1000 * 100);
	testList(1000 * 200);
	testArray(1000 * 100);
	testArray(1000 * 1000);
	testArray(1000 * 1000 * 200);
	testHash(1000 * 100);
	testHash(1000 * 1000);
	testHash(1000 * 1000 * 20);
	return nRetCode;
}

AnswerRe: MFC vs STL performance test Pin
Richard MacCutchan18-Oct-13 21:08
mveRichard MacCutchan18-Oct-13 21:08 
GeneralRe: MFC vs STL performance test Pin
Alexander Fedorov19-Oct-13 0:46
MemberAlexander Fedorov19-Oct-13 0:46 
GeneralRe: MFC vs STL performance test Pin
xenotron19-Oct-13 4:44
Memberxenotron19-Oct-13 4:44 
GeneralRe: MFC vs STL performance test Pin
Alexander Fedorov19-Oct-13 5:15
MemberAlexander Fedorov19-Oct-13 5:15 
GeneralRe: MFC vs STL performance test Pin
xenotron19-Oct-13 5:31
Memberxenotron19-Oct-13 5:31 
GeneralRe: MFC vs STL performance test Pin
Richard MacCutchan19-Oct-13 6:21
mveRichard MacCutchan19-Oct-13 6:21 
AnswerRe: MFC vs STL performance test Pin
Aescleal21-Oct-13 23:58
MemberAescleal21-Oct-13 23:58 
GeneralRe: MFC vs STL performance test Pin
Alexander Fedorov22-Oct-13 2:05
MemberAlexander Fedorov22-Oct-13 2:05 
GeneralRe: MFC vs STL performance test Pin
Aescleal22-Oct-13 4:44
MemberAescleal22-Oct-13 4:44 
GeneralRe: MFC vs STL performance test Pin
Alexander Fedorov22-Oct-13 5:32
MemberAlexander Fedorov22-Oct-13 5:32 
Answerstd::map too slow Pin
bkelly1310-Nov-13 16:08
Memberbkelly1310-Nov-13 16:08 
GeneralRe: std::map too slow Pin
Albert Holguin14-Nov-13 10:02
professionalAlbert Holguin14-Nov-13 10:02 
GeneralRe: std::map too slow Pin
Aescleal15-Nov-13 11:07
MemberAescleal15-Nov-13 11:07 
GeneralRe: std::map too slow Pin
Albert Holguin15-Nov-13 11:32
professionalAlbert Holguin15-Nov-13 11:32 
QuestionWhy calling sort() crashes? Pin
Kumar Anitesh15-Oct-13 22:38
MemberKumar Anitesh15-Oct-13 22:38 
AnswerRe: Why calling sort() crashes? Pin
Richard MacCutchan15-Oct-13 23:06
mveRichard MacCutchan15-Oct-13 23:06 
AnswerRe: Why calling sort() crashes? Pin
Theo Buys9-Sep-14 2:46
MemberTheo Buys9-Sep-14 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.