Thread: rapid histogramming

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    82

    rapid histogramming

    Hi,
    I have a prog that takes in a 2D matrix "mat", of length "cols", and depth "rows" and whose max value is mx and min value is mn, and I want to do a simple histogram of 20 "buckets". I scribbled up some code, but I'm wondering if I can't make it more efficient. It seems a little laborious to me, somehow.

    Thought I'd pop it up here to see if aybody's got any ideas. (BTW, I generally expect to get a bottom heavy histogram).

    Code:
    int *histo(double *mat, int cols, int rows, int nbu, double mx, double mn)
    {
       int i, j, k, *h;
       double dh;
       dh = (mx - mn)/ nbu; // size of each bucket / interval
       h=calloc(nbu,sizeof(int));
       for(i = 0; i < rows; i++)
          for(j = 0; j < cols; j++) {
             for(k=0;k<nbu-1;++k) {
                if(mat[i*cols+j]<(k+1)*dh+mn) {
                   h[k]++;
                   break;
                }
             }
             if(mat[i*cols+j]>=((nbu-1)*dh+mn))
                h[nbu-1]++;
          }
       return h;
    }

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Well, first pointer I can give you is that your variable names suck. Make them more descriptive, so that it's easier to figure out what each part does.
    Also, you're missing curly braces in the first for statement and the if statement at the end.
    You should also init your variables (and again give them more descriptive names).

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    82
    Hmm... well, it's a function, not a full program. Only some variables are initialized. It runs fine on gcc. I know I'm being a bit overconcise, but I don't want to go into how to do a histogram either. I thought it would make sense to somebody who had tried to do a simple histogram at some stage. "nbu" is number of buckets.

    I think 'll reply to myself and say, listen mister, go for more features, rather than more optimization .. or do it up in assembly if you're so worried about performance!

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by stabu View Post
    Hmm... well, it's a function, not a full program. Only some variables are initialized. It runs fine on gcc. I know I'm being a bit overconcise, but I don't want to go into how to do a histogram either. I thought it would make sense to somebody who had tried to do a simple histogram at some stage. "nbu" is number of buckets.

    I think 'll reply to myself and say, listen mister, go for more features, rather than more optimization .. or do it up in assembly if you're so worried about performance!
    Maybe you should generate some statictics about this function, like generate some random matrices and insert loop counters. Then isolate cases when loop counters get highest values and see which matrices are that. And finaly, try to "figure out" how to optimize that cases. So basicly, you starting from worst case scenarios (or most often ones!) if you don't have any better idea at a time.
    Last edited by rasta_freak; 08-18-2008 at 04:25 PM.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    82
    thanks for replies! I thought someone might say ++i is faster than i++, but that must be codswallop

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I thought someone might say ++i is faster than i++, but that must be codswallop
    Well for simple integers used in the context of a for loop increment, it is.

    But if you've got a class variable where the class has overloaded ++, then there really can be a difference.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    31
    For a well designed and open-source implementation of a histogram have a look here:
    http://www.gnu.org/software/gsl/manu...istograms.html.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. ahhh help....rapid keypress detection
    By technoXavage in forum Game Programming
    Replies: 1
    Last Post: 12-18-2003, 01:00 PM