#include <stdio.h>
#include <algorithm>
#include <list>
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
using std::list;
const int uiArraySize = 45;
inline int f(int i, int j, int c, int k)
{
return abs(i - c) + abs(j - k);
}
int g_testArray[uiArraySize][uiArraySize][uiArraySize][uiArraySize] = { };
void fill_array()
{
for(int i = 0; i < uiArraySize; ++i)
{
for(int j = 0; j < uiArraySize; ++j)
{
for(int c = 0; c < uiArraySize; ++c)
{
for(int k = 0; k < uiArraySize; ++k)
{
g_testArray[i][j][c][k] = f(i, j, c, k);
}
}
}
}
}
void TestArray(list<int> &results)
{
for(int i = 0; i < uiArraySize; ++i)
{
for(int j = 0; j < uiArraySize; ++j)
{
for(int c = 0; c < uiArraySize; ++c)
{
for(int k = 0; k < uiArraySize; ++k)
{
if(results.size() < 1000)
{
results.push_back(g_testArray[i][j][c][k]);
}
}
}
}
}
}
void TestInlineFunction(list<int> &results)
{
for(int i = 0; i < uiArraySize; ++i)
{
for(int j = 0; j < uiArraySize; ++j)
{
for(int c = 0; c < uiArraySize; ++c)
{
for(int k = 0; k < uiArraySize; ++k)
{
if(results.size() < 1000)
{
results.push_back(f(i, j, c, k));
}
}
}
}
}
}
int main()
{
fill_array();
{
list<int> results;
clock_t start = clock();
TestArray(results);
clock_t end = clock();
double consumed_time = (double)(end - start) / (double)CLOCKS_PER_SEC;
printf("Time for array is: %lf sec. \n", consumed_time);
}
{
list<int> results;
clock_t start = clock();
TestInlineFunction(results);
clock_t end = clock();
double consumed_time = (double)(end - start) / (double)CLOCKS_PER_SEC;
printf("Time for inline function is: %lf sec. \n", consumed_time);
}
return 0;
}