So I did a test using this code:
Test is just an empty function defined in another source file to prevent the compiler from optimizing away the function call.Code:// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
void Test();
namespace Constants
{
const int Kilo = 1000;
const int Mega = Kilo * 1000;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE h = OpenThread( THREAD_ALL_ACCESS, FALSE, GetCurrentThreadId() );
if (h == NULL)
{
std::cout << "Failed to open thread!\n";
return 1;
}
if (! SetThreadPriority(h, THREAD_PRIORITY_TIME_CRITICAL) )
{
std::cout << "Failed to set thread priority!\n";
return 1;
}
CloseHandle(h);
h = GetCurrentProcess(); //OpenProcess( GetCurrentProcessId(), FALSE, PROCESS_ALL_ACCESS );
if (h == NULL)
{
std::cout << "Failed to open process!\n";
return 1;
}
if (! SetPriorityClass(h, REALTIME_PRIORITY_CLASS) )
{
std::cout << "Failed to set process priority!\n";
return 1;
}
CloseHandle(h);
void (*pTest)() = &Test;
DWORD dwStart = GetTickCount();
for (int i = 0; i < 1000 * Constants::Mega; i++)
pTest();
std::cout << "Took " << GetTickCount() - dwStart << " ms.\n";
dwStart = GetTickCount();
for (int i = 0; i < 1000 * Constants::Mega; i++)
Test();
std::cout << "Took " << GetTickCount() - dwStart << " ms.\n";
return 0;
}
I set the process and thread priority to time critical to avoid as much outside interference as possible.
I ran the test a total of 36 times, and these are the results I got:
Function pointer (ms)
3292
3307
3307
3276
3307
3307
3308
3292
3307
3323
3307
3292
3323
3323
3339
3291
3323
3291
3323
3323
3323
3291
3323
3323
3323
3307
3292
3307
3308
3292
3323
3307
3307
3322
3338
3307
3308
Direct call (ms)
3307
3292
3308
3276
3323
3276
3307
3276
3308
3291
3292
3276
3307
3276
3307
3276
3291
3276
3307
3276
3307
3292
3308
3276
3307
3276
3291
3276
3292
3276
3276
3308
3276
3292
3276
3307
3276
Mean (ms)
Function pointer: 3310
Direct call: 3291
Standard deviation (ms)
Function pointer: 14,52609746311790
Direct call: 14,74945919506710
Standard uncertainty (ms)
Function pointer: 0,3925972287329160
Direct call: 0,3986340322991110
Conclusion: the performance impact is negligible, yet there seems to be an impact on using the function pointer, although more conclusive tests would have to be done to say exactly.