# Counting Num. of Function Executions

This is a discussion on Counting Num. of Function Executions within the C Programming forums, part of the General Programming Boards category; Let's say I use a qsort() function to "sort" a randomly generated integer array. To sort the array I use ...

1. ## Counting Num. of Function Executions

Let's say I use a qsort() function to "sort" a randomly generated integer array. To sort the array I use the compare_int() function as shown below, but I also want to count how many times I call the compare_int() function, how would I go about doing this without using any global variables?

Code:
#include <stdio.h>

int compare_int(int* x, int* y);
int count = 0;

int main(void)
{
int rand_array[35];
int i = 0;
srand(time(0));

printf("Unsorted: \n");
for (i=0; i < 35; i++)
{
rand_array[i] = rand() % 666;
printf("%d ",rand_array[i]);
}

printf("\n\n");
qsort(rand_array,35,sizeof(int),compare_int);

printf("Sorted: \n");
for (i=0; i < 35; i++)
printf("%d ",rand_array[i]);

printf("\n%d comparisons used\n",count);

return 0;
}

int compare_int(int* x, int* y)
{
count++;
if (*x > *y)
return 1;
else if (*x < *y)
return -1;
else
return 0;
}
That method uses a global variable count, and I simply just increment count every time I execute the compare_int() function. How do I go about doing the same thing without using the global var count? I've tried using extern/static modifiers to no luck, any hints would be greatly appreciated

2. You could pass the address of a count int to the function, by changing it like so:-
Code:
int compare_int(int* x, int* y, int* count)
{
// At the top of the function...
(*count)++;
}
Make sure that the int that you pass is set to 0 before you start though.

EDIT: No, that probably won't work given that it seems to be called by qsort(), which is likely to expect a function in that original form. I don't think that there's another way you could do it, short of making your own qsort function.

3. Hmm theres supposed to be a way to do this but argh not sure
Nah the problem is to use the standard qsort() definition so you can't just make your own otherwise that'll be too easy

4. a bit hackish, but....
Code:
int count_int(int *x, int *y)
{
mycount();
if/then
.............

return whatever;
}

int mycount(void)
{
static int counter = 0;
counter++;
return counter;
}

int main()
{
qsort(....);
cout << "it was called" << mycount();	<< "times";
}
without modification, this works only once per program

5. for some unknown reason this has had me intrigued. i've revised my original solution and packaged it up. as always, i haven't tested it, but here it is...

Code:
int compare_int(int *x, int *y)
{
count();
if(x < y)
return -1;
if(x > y)
return +1;
return 0;
}

int * count(void)
{
static int counter = 0;
counter++;
return &counter;
}

int qsort2(int *a, int b, int c, int (*d)(int, int))
{

int temp;
int *p = count();	//get address of counter in count function
*p = 0;			//initialize it to 0 because we just made it count up 1
qsort(a,b,c,d);         //call quick sort which will call compare int
//which will call count
temp = *p;		//move to temp so we can reset counter to 0
*p = 0;			//..so that other functions can use the count()
//without having to initialize counter to 0
return temp;

}

int main()
{
int comparisons;
comparisons = qsort2(w,x,y,compare_int);
printf("There were %d comparisons made\n", comparisons);
}

6. Your first one was quite sufficient since I only needed it to run once per program
Pretty neat - I wouldn't of thought of creating a separate function

7. What if you want to find out the count without incrementing it? Instead of making count void, pass it a Boolean that increments when true but simply returns the count if false.

8. Here's the first actual error/warning -free version of this thing:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int compare_int(const void *x, const void *y);
int count(int inc);

int main(void)
{
int rand_array[35];
int i = 0;

srand(time(0));

printf("Unsorted: \n");
for(i = 0;i < 35;++i)
{
rand_array[i] = rand() % 666;
printf("%d ", rand_array[i]);
}

printf("\n\n");
qsort(rand_array, 35, sizeof(int), compare_int);

printf("Sorted: \n");
for(i = 0;i < 35;++i)
printf("%d ", rand_array[i]);

printf("\n\n");
printf("count() was called %d times\n", count(0));

return 0;
}

int compare_int(const void *x, const void *y)
{
count(1);

if(*(int *)x > *(int *)y)
return 1;
else if(*(int *)x < *(int *)y)
return -1;
return 0;
}

int count(int inc)
{
static int ctr = 0;

if(inc)
ctr++;
return ctr;
}
Unsorted:
456 372 468 145 343 657 360 556 550 322 582 356 364 176 532 206 514 590 449 215 616 168 208 601 248 626 277 341 580 59 612 423 431 414 568

Sorted:
59 145 168 176 206 208 215 248 277 322 341 343 356 360 364 372 414 423 431 449 456 468 514 532 550 556 568 580 582 590 601 612 616 626 657

count() was called 141 times

9. Most implementations support a C profiler - profilers usually call the monitor family of functions which track the number of times a function address is entered, and the total time spent in the function.

check out mon.h - if you are on unix try man monitor - you can get very fine control over what you profile that way.