Click here to Skip to main content
15,886,689 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
Questionstl thread safe Pin
G.aius.CodeCreator18-Nov-13 15:38
G.aius.CodeCreator18-Nov-13 15:38 
Questionwaiting on events for socket activity Pin
bkelly1318-Nov-13 14:05
bkelly1318-Nov-13 14:05 
AnswerRe: waiting on events for socket activity Pin
Orjan Westin19-Nov-13 23:03
professionalOrjan Westin19-Nov-13 23:03 
GeneralRe: waiting on events for socket activity Pin
bkelly1325-Nov-13 16:08
bkelly1325-Nov-13 16:08 
GeneralRe: waiting on events for socket activity Pin
Orjan Westin25-Nov-13 23:01
professionalOrjan Westin25-Nov-13 23:01 
GeneralRe: waiting on events for socket activity Pin
bkelly1326-Nov-13 8:07
bkelly1326-Nov-13 8:07 
QuestionWhat is the window argument in WSAAsyncSelect(...) Pin
bkelly1310-Nov-13 15:59
bkelly1310-Nov-13 15:59 
AnswerRe: What is the window argument in WSAAsyncSelect(...) Pin
Richard MacCutchan10-Nov-13 21:08
mveRichard MacCutchan10-Nov-13 21:08 
Generalcreate a window for callbacks Pin
bkelly1311-Nov-13 2:00
bkelly1311-Nov-13 2:00 
GeneralRe: create a window for callbacks Pin
Richard MacCutchan11-Nov-13 3:01
mveRichard MacCutchan11-Nov-13 3:01 
GeneralRe: create a window for callbacks Pin
bkelly1311-Nov-13 4:49
bkelly1311-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
bkelly1311-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
bkelly1312-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
bkelly1321-Nov-13 14:51 
QuestionMFC vs STL performance test Pin
Alexander Fedorov18-Oct-13 11:42
Alexander 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
Alexander Fedorov19-Oct-13 0:46 
GeneralRe: MFC vs STL performance test Pin
pasztorpisti19-Oct-13 4:44
pasztorpisti19-Oct-13 4:44 
GeneralRe: MFC vs STL performance test Pin
Alexander Fedorov19-Oct-13 5:15
Alexander Fedorov19-Oct-13 5:15 
GeneralRe: MFC vs STL performance test Pin
pasztorpisti19-Oct-13 5:31
pasztorpisti19-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
Aescleal21-Oct-13 23:58 

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.