T
Thomas Kowalski
Hello everybody,
currently I am tweaking a bit on one of my projects and wondered how
high are the costs for calling a (member) function. I tried some
possibilites and come to a suprising result.
Maybe someone can explain to me what I might improve on my test or how
come this results occured?
For testing I commented different lines out. Code see below.
Regards,
Thomas Kowalski
// MS C++ 8.0
// 1 Billion additions:
// simple methode call: 2134 ms, 2123 ms,
// inline call: 2037 ms, 2053 ms, 2073 ms
// simple methode ref param: 2153 ms, 2034 ms, 2093 ms
// addition no methode 2034 ms, 2037 ms, 2032 ms
// virtual funtion call ~2100 ms
// function pointer 7060 ms, 7260 ms, 7260 ms
// Results with gcc 3.3.6 are similar but function pointer are as fast
as normal calls
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <vector>
#include <time.h>
using namespace std;
class Abstract {
public:
virtual size_t add(size_t& a, size_t& b) = 0;
};
class B ublic Abstract {
public:
virtual size_t add(size_t& a, size_t& b);
};
class C ublic Abstract {
public:
virtual size_t add(size_t& a, size_t& b);
};
class D ublic Abstract {
public:
virtual size_t add(size_t& a, size_t& b);
};
/**/
class A {
public:
static size_t add(size_t& a, size_t& b);
};
//inline
inline size_t A::add(size_t& a, size_t& b) {
return a+b;
};
size_t B::add(size_t& a, size_t& b) {
return a+b;
};
size_t C::add(size_t& a, size_t& b) {
return a+b;
};
size_t D::add(size_t& a, size_t& b) {
return a+b;
};
int main(int argc, char *argv[])
{
// Abstract* a = new D();
A a;
size_t (*addition)(size_t&, size_t&);
addition = &A::add;
const size_t REPEAT = 1000000000;
size_t i,j,k;
j=5;
clock_t start = clock();
for (i=0; i<REPEAT; ++i) {
addition(i, k);
// k = a.add(i,j);
// k = i+j;
}
clock_t ende = clock();
cout << "K ist: " << k << endl;
cout << "test took " << ((ende - start)) << " ms" << endl;
cin >> i;
return 0;
}
currently I am tweaking a bit on one of my projects and wondered how
high are the costs for calling a (member) function. I tried some
possibilites and come to a suprising result.
Maybe someone can explain to me what I might improve on my test or how
come this results occured?
For testing I commented different lines out. Code see below.
Regards,
Thomas Kowalski
// MS C++ 8.0
// 1 Billion additions:
// simple methode call: 2134 ms, 2123 ms,
// inline call: 2037 ms, 2053 ms, 2073 ms
// simple methode ref param: 2153 ms, 2034 ms, 2093 ms
// addition no methode 2034 ms, 2037 ms, 2032 ms
// virtual funtion call ~2100 ms
// function pointer 7060 ms, 7260 ms, 7260 ms
// Results with gcc 3.3.6 are similar but function pointer are as fast
as normal calls
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <vector>
#include <time.h>
using namespace std;
class Abstract {
public:
virtual size_t add(size_t& a, size_t& b) = 0;
};
class B ublic Abstract {
public:
virtual size_t add(size_t& a, size_t& b);
};
class C ublic Abstract {
public:
virtual size_t add(size_t& a, size_t& b);
};
class D ublic Abstract {
public:
virtual size_t add(size_t& a, size_t& b);
};
/**/
class A {
public:
static size_t add(size_t& a, size_t& b);
};
//inline
inline size_t A::add(size_t& a, size_t& b) {
return a+b;
};
size_t B::add(size_t& a, size_t& b) {
return a+b;
};
size_t C::add(size_t& a, size_t& b) {
return a+b;
};
size_t D::add(size_t& a, size_t& b) {
return a+b;
};
int main(int argc, char *argv[])
{
// Abstract* a = new D();
A a;
size_t (*addition)(size_t&, size_t&);
addition = &A::add;
const size_t REPEAT = 1000000000;
size_t i,j,k;
j=5;
clock_t start = clock();
for (i=0; i<REPEAT; ++i) {
addition(i, k);
// k = a.add(i,j);
// k = i+j;
}
clock_t ende = clock();
cout << "K ist: " << k << endl;
cout << "test took " << ((ende - start)) << " ms" << endl;
cin >> i;
return 0;
}