# Thread: rapid histogramming

1. ## 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. 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

3. 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. Originally Posted by stabu
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.

5. thanks for replies! I thought someone might say ++i is faster than i++, but that must be codswallop

6. > 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.

7. 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