Originally Posted by
Elysia
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.
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:
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.
Originally Posted by
Visual Studio
1>------ Build started: Project: OpenMP Sandbox, Configuration: No Debug Info Win32 ------
1>Compiling...
1>main.cpp
1>Linking...
1>LINK : warning LNK4224: /OPT:NOWIN98 is no longer supported; ignored
1>main.obj : error LNK2001: unresolved external symbol "void __cdecl Test(void)" (?Test@@YAXXZ)
1>E:\Projects\Double Vision Recorder\No Debug Info\OpenMP Sandbox.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://e:\Projects\Double Vision Recorder\OpenMP Sandbox\No Debug Info\BuildLog.htm"
1>OpenMP Sandbox - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Odd that it wouldn't even compile. Ah I see well, after I actually defined test, it failed to open the thread, so perhaps the code is a bit buggy. Change that monstrosity at the start to this -
Code:
if (! SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) ){
std::cout << "Failed to set thread priority!\n";
return 1;
}
Here, i cleaned it up and among other things removed the part that includes the allocation fo the local variable as a penalty to the pointer routine. probably negligeable but its bad form.
Code:
#include <windows.h>
#include <iostream>
void Test(){
return;
}
namespace Constants
{
const int Kilo = 1000;
const int Mega = Kilo * 1000;
}
int main(int argc, char* argv[]){
if (! SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) ){
std::cout << "Failed to set thread priority!\n";
return 1;
}
if (! SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) ){
std::cout << "Failed to set process priority!\n";
return 1;
}
void (*pTest)() = &Test;
DWORD dwStartpFunc , dwStartFunc;
DWORD dwStoppFunc , dwStopFunc;
dwStartpFunc = GetTickCount();
for (int i = 0; i < 1000 * Constants::Mega; i++) pTest();
dwStoppFunc = GetTickCount();
std::cout << "Took " << dwStoppFunc - dwStartpFunc << " ms.\n";
dwStartFunc = GetTickCount();
for (int i = 0; i < 1000 * Constants::Mega; i++) Test();
dwStopFunc = GetTickCount();
std::cout << "Took " << dwStopFunc - dwStartFunc << " ms.\n";
return 0;
}
and apparently I cant get VS2008 to stop optimizing the function call, either that or it claims my computer is performing one billion increments in less than 15ms
Ok, the problem came down to forcing a rebuild. And teh results i got where below the timer resolution.
Here is the final code -
Test.cpp
Code:
extern unsigned long Count;
void Test(unsigned long* Junk){
*Junk+=2;
return;
}
main.cpp
Code:
#include <windows.h>
#include <iostream>
extern void Test(unsigned long*);
unsigned long Junk = 0;
unsigned long Count = 1000000000;
namespace Constants
{
const int Kilo = 1000;
const int Mega = Kilo * 1000;
}
int main(int argc, char* argv[]){
if (! SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) ){
std::cout << "Failed to set thread priority!\n";
return 1;
}
if (! SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) ){
std::cout << "Failed to set process priority!\n";
return 1;
}
for(int y = 0;y<10;y++){
// first we make sure the code for test() is in teh cache, so the pointer routine doesnt getpenalized
for(int x = 0;x<1000;x++) Test(&Junk);
void (*pTest)(unsigned long*) = &Test;
DWORD dwStartpFunc , dwStartFunc;
DWORD dwStoppFunc , dwStopFunc;
dwStartpFunc = GetTickCount();
for (int i = 0; i < Count; i++) pTest(&Junk);
dwStoppFunc = GetTickCount();
std::cout << "pFunc Took " << dwStoppFunc - dwStartpFunc << " ms.\n";
dwStartFunc = GetTickCount();
for (int i = 0; i < Count; i++) Test(&Junk);
dwStopFunc = GetTickCount();
std::cout << "Func Took " << dwStopFunc - dwStartFunc << " ms.\n";
std::cout << "\n";
}
return 0;
}
results indicate that there is no zero, or very little difference in speed of either method.