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. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    173

    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. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,266
    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.
    Last edited by SMurf; 04-15-2005 at 05:40 AM.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    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. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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);
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    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. #7
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    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.
    MSVC++ 6.0

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,170
    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
    If you understand what you're doing, you're not learning anything.

  9. #9
    .
    Join Date
    Nov 2003
    Posts
    307
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21