Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointer to function executor

Started by wookie1 Sep 20, 2010 at 10:47 AM 4 replies 1.1k views
Original Post
wookie1
wookie1
What I'm trying to do may well be impossible but I'm interested now.
I'm trying to write a small function that will test the speed of a function that is passed to it.
In order to make this worthwhile it need to be as generic as possible. For this reason I'd like a way of passing a pointer to a function execution if that makes sense. So if I had
function( variable1, variable2);
normally returning something I want to pass a pointer to this execution. Something like this.

template
void test(*func(), int i_max)
{
clock_t begin = clock();

for (int i = 0; i<= i_max; ++i)
{
T temp = *funct;
}
clock_t end = clock();

cout<< "Iterations: "<< i_max <<" Time: "<<(end - begin) <}

Then to use you would call:
test(&function, 100000);

Apologies if this is a stupid question but I feel there out to be some clever method to do this.
swiftcoder
swiftcoder
I would be inclined to use boost::function and boost::bind (or their std::tr1 equivalents) to achieve this. If you attempt to do this in the low-level fashion you show there, you are going to have issues with functions taking different numbers of arguments.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
wookie1
wookie1
That's great exactly what I was looking for thanks.
EvincarOfAutumn
EvincarOfAutumn
Using C++0x, you can do this with a variadic template:

template<typename F, typename... Args>void test(F function, int max, const Args&... args) {    clock_t begin = clock();    for (int i = 0; i < max; ++i)        function(args...);    clock_t end = clock();    // ...}

KulSeran
KulSeran
Quote:

Apologies if this is a stupid question but I feel there out to be some clever method to do this.

Your method may be clever, but it isn't going to produce accurate results. The best way to find out how much time a function takes is to use it in a real world scenario, and then run a profiler over your application. Here you are fighting several things.

One, your function has no chance of being inlined in your test. It could be running a lot faster than you think, but will be held back due to how you are calling it.
Two, your function is being run over-and-over-and-over. It is already in the cache, and so is all its data. Your function is running a lot faster than it would in a real scenario where the data isn't always in the cache.
Three, if you solve 1, then there is a good chance that your loop, as written, will get optimized away, again resulting in abnormally fast test runs.
wookie1
wookie1
I appreciate it may have little real use when compared to a profiler I just was doing it as an interesting exercise as much as anything else. I don't have time to write large programs so I was just looking for a way to test similar functions and output the results in a nice standardized way.
Anyway here's what I've come up with, I'd appreciate any feed back on My code. I'm not using from a separate source file or anything just got it set up as a template, so I can play fill in the blanks whenever I feel the need.

#include <boost/function.hpp>#include <iostream>#include "boost/bind.hpp"class function{public:	function(int a, std::string n): var_1(a), name(n) {}	function(int a, std::string n, boost::function<void()> f): var_1(a), name(n), f_(f) {}		void operator() ()	{	  if (f_)	  {      f_();      }	}	template <typename Func> void set_function(Func f)	{    f_=f;  	}	void i_checker()	{	// checks other than speed may be done on the function here	std::cout<<var_1<<std::endl;	}	void get_name()	{	std::cout<<"Function: "<< name<<std::endl;	}	private:	int var_1;	std::string name;	boost::function<void()> f_;};void checker(function * f, int i_max ){	// Checks and writes the function execution time and calls any other checks	clock_t begin = clock();	for (int i = 0; i<= i_max; ++i)	{		(*f)();	}	clock_t end = clock();	f->get_name();	f->i_checker();	float time = end-begin;	float time_1000 = (time/i_max) *1000;	std::cout<< "Iterations: "<< i_max <<" Time: "<<time << " Time for 1000: "<< time_1000<< std::endl;}int main(){function f(1, "Name", boost::bind(&desired_function, &functions object, other_variables));checker (&f, number_of_iterations);return 0;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.