[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: mplayer



[ lots of discussion about function pointer slowdown ]

Matthias Urlichs wrote:
> If the function-pointerized code just looks ugly, the C preprocessor is
> your friend -- on the other hand you don't even need that, as foo(bar,...)
> is equivalent to (*foo)(bar,...) if foo is a pointer to a function.

I`ve crafted a piece of code which hopefully addresses the question.
I`m not sure though if gcc was able to out-smart little me trying to
blow out its attempts to optimise the code 8)

so here it is:
-----------------------( moo.c
int a(int p)
{
	int ret = 0, i;
	
	for (i = 0; i < p; i++)
		ret += i;

	return ret;
}

int b(int p)
{
	int ret = 0, i;
	
	for (i = 0; i < p; i++)
		ret -= i;

	return ret;
}

int main(int argc, char **argv)
{
	int (*fn)(int parm);

	if (argc < 2) {
		printf("parm count error: %d\n", argc);
		exit(-1);
	}

	switch (argv[1][0]) {
	case 'a':
		fn = a;
		printf("%d\n", fn(1000000000));
		break;
	case 'b':
		fn = b;
		printf("%d\n", fn(1000000000));
		break;
	case 'c':
		fn = a;
		printf("%d\n",  a(1000000000));
		break;
	default:
		printf("parm value error\n");
		exit(-1);
	}

	return 0;
}

here are the outputs:
-----------------------( p3-500
[deepfire@drakkar:~/datasets/virtualitty]# gcc ./moo.c -o moo -O2
[deepfire@drakkar:~/datasets/virtualitty]# time ./moo a
-1243309312

real    0m6.088s
user    0m6.085s
sys     0m0.003s
[deepfire@drakkar:~/datasets/virtualitty]# time ./moo b
1243309312

real    0m4.060s
user    0m4.058s
sys     0m0.001s
[deepfire@drakkar:~/datasets/virtualitty]# time ./moo c
-1243309312

real    0m6.088s
user    0m6.083s
sys     0m0.004s
[deepfire@drakkar:~/datasets/virtualitty]#


Probably i just was misled, probably gcc outsmarted me...

Just my two kopecks.


regards, Samium Gromoff



Reply to: