Crap code performing better than refactored code

Hi guys

I refactored a big mess of a function and for some reason the refactored version is REAAALY slow relative to the messuy one. I expected it to be a little slower because of all the function calls (which should be optimized away by the compiler), but it is much to slow.

I ran both version through gprof but it says they are relatively close in time. Yet I have to wait for the refactored version to finish while the messy version finishes almost instantly.

The same main will exercise both versions.

The clean version (I manually inlined all of my functions):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <cstdio>
#include <cstdlib>
#include <stdint.h>
#include <cmath>

#define RED_OFFSET 0
#define GREEN_OFFSET 1
#define BLUE_OFFSET 2

enum SIGNATURE {
    RGBMEAN, RGBVAR, RGBSKU, RGBKER, RGBVEC,
    RMEAN, RVAR, RSKU, RKER, RVEC,
    GMEAN, GVAR, GSKU, GKER, GVEC,
    BMEAN, BVAR, BSKU, BKER, BVEC
};

void computeSignature(uint8_t *RGBimage, int width, int height, int useEntropy, double *signature);

int main(){
    int i;
    uint8_t data[3000000];
    double signature[20];

     for ( i = 0; i < 3000000; i += 3) {
         data[i] = i;
         data[i + 1] = i % 127;
         data[i + 2] = i * 2;
     }

     ALTcomputeSignature(data, 1000, 1000, 0, signature);
     ALTcomputeSignature(data, 1000, 1000, 1, signature);

    printf("DONE\n");
}

double x_log2_x(uint64_t x){
    return (x==0) ? 0 : (double)x*log2((double)x); //use log(x)/log(2.) if this doesnt work
}

void computeSignature(uint8_t *RGBimage, int width, int height, int useEntropy, double *signature) {
    size_t i;
    size_t size = width*height;
    uint8_t *grayImage = (uint8_t*) malloc(size*sizeof(uint8_t));
    uint8_t *data;

    uint64_t redHistogram[256], greenHistogram[256], blueHistogram[256], grayHistogram[256];
    double  redEntropy[256], greenEntropy[256], blueEntropy[256], grayEntropy[256];

    for(i=0; i<20; i++) signature[i] = 0;

    for(i = 0, data = RGBimage; i < size; i++, data += 3)
        grayImage[i] = (uint8_t) ((double)(data[RED_OFFSET] + data[GREEN_OFFSET] + data[BLUE_OFFSET]) / 3);

    for (i = 0, data = RGBimage; i < size; i++, data += 3){
        grayHistogram[ grayImage[i] ]++;
        redHistogram[ data[ RED_OFFSET ] ]++;
        greenHistogram[ data[ GREEN_OFFSET ] ]++;
        blueHistogram[ data[ BLUE_OFFSET ] ]++;
    }

    if (useEntropy) {
        for (i = 0; i < 256; i++){
            grayEntropy[i] = x_log2_x(grayHistogram[i]);
            redEntropy[i] = x_log2_x(redHistogram[i]);
            greenEntropy[i] = x_log2_x(greenHistogram[i]);
            blueEntropy[i] = x_log2_x(blueHistogram[i]);
        }

        for (i = 0, data = RGBimage; i < size; i++, data += 3) {
            signature[RGBMEAN] += grayEntropy[grayImage[i]];
            signature[RMEAN] += redEntropy[data[RED_OFFSET]];
            signature[GMEAN] += greenEntropy[data[GREEN_OFFSET]];
            signature[BMEAN] += blueEntropy[data[RED_OFFSET]];
        }
        signature[RGBMEAN] /= size;
        signature[RMEAN] /= size;
        signature[GMEAN] /= size;
        signature[BMEAN] /= size;

        for (i = 0, data = RGBimage; i < size; i++, data += 3) {
            signature[RGBVAR] += pow(grayEntropy[grayImage[i]] - signature[RGBMEAN], 2.);
            signature[RVAR] += pow(redEntropy[data[RED_OFFSET]] - signature[RMEAN], 2.);
            signature[GVAR] += pow(greenEntropy[data[GREEN_OFFSET]] - signature[GMEAN], 2.);
            signature[BVAR] += pow(blueEntropy[data[RED_OFFSET]] - signature[BMEAN], 2.);

            signature[RGBSKU] += pow(grayEntropy[grayImage[i]] - signature[RGBMEAN], 3.);
            signature[RSKU] += pow(redEntropy[data[RED_OFFSET]] - signature[RMEAN], 3.);
            signature[GSKU] += pow(greenEntropy[data[GREEN_OFFSET]] - signature[GMEAN], 3.);
            signature[BSKU] += pow(blueEntropy[data[RED_OFFSET]] - signature[BMEAN], 3.);

            signature[RGBKER] += pow(grayEntropy[grayImage[i]] - signature[RGBMEAN], 4.);
            signature[RKER] += pow(redEntropy[data[RED_OFFSET]] - signature[RMEAN], 4.);
            signature[GKER] += pow(greenEntropy[data[GREEN_OFFSET]] - signature[GMEAN], 4.);
            signature[BKER] += pow(blueEntropy[data[RED_OFFSET]] - signature[BMEAN], 4.);
        }

        for (i = 0; i < 256; i++) {
            signature[RGBVEC] += pow(grayEntropy[i], 2.);
            signature[RVEC] += pow(redEntropy[i], 2.);
            signature[GVEC] += pow(greenEntropy[i], 2.);
            signature[BVEC] += pow(blueEntropy[i], 2.);
        }
    }else{
        for (i = 0, data = RGBimage; i < size; i++, data += 3) {
            signature[RGBMEAN] += grayImage[i];
            signature[RMEAN] += data[RED_OFFSET];
            signature[GMEAN] += data[GREEN_OFFSET];
            signature[BMEAN] += data[RED_OFFSET];
        }
        signature[RGBMEAN] /= size;
        signature[RMEAN] /= size;
        signature[GMEAN] /= size;
        signature[BMEAN] /= size;

        for (i = 0, data = RGBimage; i < size; i++, data += 3) {
            signature[RGBVAR] += pow(grayImage[i] - signature[RGBMEAN], 2.);
            signature[RVAR] += pow(data[RED_OFFSET] - signature[RMEAN], 2.);
            signature[GVAR] += pow(data[GREEN_OFFSET] - signature[GMEAN], 2.);
            signature[BVAR] += pow(data[RED_OFFSET] - signature[BMEAN], 2.);

            signature[RGBSKU] += pow(grayImage[i] - signature[RGBMEAN], 3.);
            signature[RSKU] += pow(data[RED_OFFSET] - signature[RMEAN], 3.);
            signature[GSKU] += pow(data[GREEN_OFFSET] - signature[GMEAN], 3.);
            signature[BSKU] += pow(data[RED_OFFSET] - signature[BMEAN], 3.);

            signature[RGBKER] += pow(grayImage[i] - signature[RGBMEAN], 4.);
            signature[RKER] += pow(data[RED_OFFSET] - signature[RMEAN], 4.);
            signature[GKER] += pow(data[GREEN_OFFSET] - signature[GMEAN], 4.);
            signature[BKER] += pow(data[RED_OFFSET] - signature[BMEAN], 4.);
        }

        for (i = 0; i < 256; i++) {
            signature[RGBVEC] += pow(grayHistogram[i], 2.);
            signature[RVEC] += pow(redHistogram[i], 2.);
            signature[GVEC] += pow(greenHistogram[i], 2.);
            signature[BVEC] += pow(blueHistogram[i], 2.);
        }
    }
    signature[RGBVAR] /= size;
    signature[RVAR] /= size;
    signature[GVAR] /= size;
    signature[BVAR] /= size;

    signature[RGBSKU] /= (size * pow(sqrt(signature[RGBVAR]), 3.));
    signature[RSKU] /= (size * pow(sqrt(signature[RVAR]), 3.));
    signature[GSKU] /= (size * pow(sqrt(signature[GVAR]), 3.));
    signature[BSKU] /= (size * pow(sqrt(signature[BVAR]), 3.));

    signature[RGBKER] /= (size * pow(sqrt(signature[RGBVAR]), 4.));
    signature[RKER] /= (size * pow(sqrt(signature[RVAR]), 4.));
    signature[GKER] /= (size * pow(sqrt(signature[GVAR]), 4.));
    signature[BKER] /= (size * pow(sqrt(signature[BVAR]), 4.));

    signature[RGBVEC] = sqrt(signature[RGBVEC]) / (256.0);
    signature[RVEC] = sqrt(signature[RVEC]) / (256.0);
    signature[GVEC] = sqrt(signature[GVEC]) / (256.0);
    signature[BVEC] = sqrt(signature[BVEC]) / (256.0);

    free(grayImage);
}


messy version (be warned, there are many lines of code here that do not contribute):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
void computeSignatureAlternate(uint8_t *dataorg, int width, int height, int roff, int goff, int boff, int option_sig, double *data_sig){
  int i,j;
  int idx;
  long rh[256], gh[256], bh[256],rgbh[256];
  double xre[256], xge[256], xbe[256], xrgbe[256];
  static int *rgbd = NULL;
  long rsum;
  long gsum;
  long bsum;
  double xlog2, dtmp;
  double rgbepnt, rgbevec, rgbevar, rgbesig, rgbesku, rgbeker;
  double repnt, revec, revar, resig, resku, reker;
  double gepnt, gevec, gevar, gesig, gesku, geker;
  double bepnt, bevec, bevar, besig, besku, beker;
  double onethird;
  long rgbsum;
  double remin, remax;
  double gemin, gemax;
  double bemin, bemax;
  double rgbemin, rgbemax;
  static long size;
  uint8_t *data;
  int *ired;
  int *igreen;
  int *iblue;
  double xfac;


  size=width*height;
  rgbd=(int *)malloc(size*sizeof(*rgbd));

  memset(rh, 0, sizeof(rh));
  memset(gh, 0, sizeof(gh));
  memset(bh, 0, sizeof(bh));
  memset(rgbh, 0, sizeof(rgbh));
  memset(xre, 0, sizeof(xre));
  memset(xge, 0, sizeof(xge));
  memset(xbe, 0, sizeof(xbe));
  memset(xrgbe, 0, sizeof(xrgbe));
  // Build Histogram...
  onethird=1.0/3.0;

  //Build the gray scale version of the image
  data=dataorg;
  for(i=0; i<size; i++) {
    // Accumulate blue for grey scale and increment the histogram count
    idx=*(data+boff);
    bh[idx]++;
    rgbd[i] = idx;
    // Accumulate green for grey scale and increment the histogram count
    idx=*(data+1);
    gh[idx]++;
    rgbd[i] += idx;
    // Accumulate red for grey scale and increment the histogram count
    idx=*(data+roff);
    //Increment the histogram and set the intensity to (R+G+B)/3.0
    rh[idx]++;
    rgbd[i] += idx;
    rgbd[i] = (int)(((double)rgbd[i])*onethird);
    rgbh[rgbd[i]]++;
    data += 3;
  }

  rsum=gsum=bsum=size;

  rgbsum=size*3;
  rgbepnt=repnt=gepnt=bepnt=0.0;
  rgbevar=revar=gevar=bevar=0.0;
  rgbesku=resku=gesku=besku=0.0;
  rgbeker=reker=geker=beker=0.0;
  rgbemin=remin=gemin=bemin=1.0e+30;
  rgbemax=remax=gemax=bemax=-1.0e+30;
  data=dataorg;

  xlog2=log10(2.0);
  if(option_sig == 0) {
    // The following is the normalization factor to convert the histogram distribution into a normalized
    //    probability distribution.
    //    xfac=1.0/size;
    xfac=1.0/1.0;
    for (i=0;i<256;i++) {
      xrgbe[i]=(double)rgbh[i]*xfac;
      xre[i]=(double)rh[i]*xfac;
      xge[i]=(double)gh[i]*xfac;
      xbe[i]=(double)bh[i]*xfac;
    }
    for (i=0;i<256;i++) {
      dtmp=(double)(xrgbe[i]);
      xrgbe[i]=-dtmp*log10(1.0/(dtmp+1.0e-100))/xlog2;
      //      printf("Histogram: %d %d %f %f\n",i,rgbh[i],dtmp,xrgbe[i]);
      dtmp=(double)(xre[i]);
      xre[i]=-dtmp*log10(1.0/(dtmp+1.0e-100))/xlog2;
      dtmp=(double)(xge[i]);
      xge[i]=-dtmp*log10(1.0/(dtmp+1.0e-100))/xlog2;
      dtmp=(double)(xbe[i]);
      xbe[i]=-dtmp*log10(1.0/(dtmp+1.0e-100))/xlog2;
    }
  } else {
    for (i=0;i<256;i++) {
      xrgbe[i]=0.0;
    }
  }
  data=dataorg;
  for(i=0; i < size; i++) {
    idx=rgbd[i];
    if(rgbh[idx] > 0) {
      /*      xfac = -table[rgbh[idx]]; */
      if(option_sig == 0) {
	xfac=xrgbe[idx];
      } else {
	xfac=idx;
      }
      rgbepnt = rgbepnt + xfac;
      if (xfac < rgbemin)
	rgbemin=xfac;
      if (xfac > rgbemax)
	rgbemax=xfac;
    }
    idx=*(data+roff);
    if(rh[idx] > 0) {
      /*      xfac = -table[tmp+tmp+tmp]; */
      if(option_sig == 0) {
	xfac=xre[idx];
      } else {
	xfac=idx;
      }
      repnt+=xfac;
      if (xfac < remin)
	remin=xfac;
      if (xfac > remax)
	remax=xfac;
    }
    idx=*(data+1);
    if(gh[idx] > 0) {
      /*      xfac = -table[tmp+tmp+tmp]; */
      if(option_sig == 0) {
	xfac=xge[idx];
      } else {
	xfac=idx;
      }
      gepnt+=xfac;
      if(xfac < gemin)
	gemin=xfac;
      if(xfac > gemax)
	gemax=xfac;
    }
    idx=*(data+boff);
    if(bh[idx] > 0) {
      /*      xfac = -table[tmp+tmp+tmp]; */
      if(option_sig == 0) {
	xfac=xbe[idx];
      } else {
	xfac=idx;
      }
      bepnt+=xfac;
      if(xfac < bemin)
	bemin=xfac;
      if(xfac > bemax)
	bemax=xfac;
    }
    data+=3;
  }

  double rgbemu, remu, gemu, bemu;
  rgbemu=rgbepnt/size;
  remu=repnt/size;
  gemu=gepnt/size;
  bemu=bepnt/size;
  //  printf("Value: %d  %d %f %f %f %f %f\n",option_sig,size,rgbepnt,rgbemu,remu,gemu,bemu);
  //  printf("Value: %f %f\n",rgbemin,rgbemax);
  data=dataorg;
  for(i=0; i < size; i++) {
    idx=rgbd[i];
    if(rgbh[idx] > 0) {
      /*      xfac = -table[rgbh[idx]]; */
      if(option_sig == 0) {
	xfac=xrgbe[idx];
      } else {
	xfac=idx;
      }
      rgbevar+=(xfac-rgbemu)*(xfac-rgbemu);
      rgbesku+=(xfac-rgbemu)*(xfac-rgbemu)*(xfac-rgbemu);
      rgbeker+=(xfac-rgbemu)*(xfac-rgbemu)*(xfac-rgbemu)*(xfac-rgbemu);
      if (xfac < rgbemin)
	rgbemin=xfac;
      if (xfac > rgbemax)
	rgbemax=xfac;
    }
    idx=*(data+roff);
    if(rh[idx] > 0) {
      /*      xfac = -table[tmp+tmp+tmp]; */
      if(option_sig == 0) {
	xfac=xre[idx];
      } else {
	xfac=idx;
      }
      revar+=(xfac-remu)*(xfac-remu);
      resku+=(xfac-remu)*(xfac-remu)*(xfac-remu);
      reker+=(xfac-remu)*(xfac-remu)*(xfac-remu)*(xfac-remu);
      if (xfac < remin)
	remin=xfac;
      if (xfac > remax)
	remax=xfac;
    }
    idx=*(data+1);
    if(gh[idx] > 0) {
      /*      xfac = -table[tmp+tmp+tmp]; */
      if(option_sig == 0) {
	xfac=xge[idx];
      } else {
	xfac=idx;
      }
      gevar+=(xfac-gemu)*(xfac-gemu);
      gesku+=(xfac-gemu)*(xfac-gemu)*(xfac-gemu);
      geker+=(xfac-gemu)*(xfac-gemu)*(xfac-gemu)*(xfac-gemu);
      if(xfac < gemin)
	gemin=xfac;
      if(xfac > gemax)
	gemax=xfac;
    }
    idx=*(data+boff);
    if(bh[idx] > 0) {
      /*      xfac = -table[tmp+tmp+tmp]; */
      if(option_sig == 0) {
	xfac=xbe[idx];
      } else {
	xfac=idx;
      }
      bevar+=(xfac-bemu)*(xfac-bemu);
      besku+=(xfac-bemu)*(xfac-bemu)*(xfac-bemu);
      beker+=(xfac-bemu)*(xfac-bemu)*(xfac-bemu)*(xfac-bemu);
      if(xfac < bemin)
	bemin=xfac;
      if(xfac > bemax)
	bemax=xfac;
    }
    data+=3;
  }

  rgbevar=rgbevar/size;
  revar=revar/size;
  gevar=gevar/size;
  bevar=bevar/size;
  rgbesig=sqrt(rgbevar)+1.0e-30;
  resig=sqrt(revar)+1.0e-30;
  gesig=sqrt(gevar)+1.0e-30;
  besig=sqrt(bevar)+1.0e-30;
  rgbesku=rgbesku/(size*rgbesig*rgbesig*rgbesig);
  resku=resku/(size*resig*resig*resig);
  gesku=gesku/(size*gesig*gesig*gesig);
  besku=besku/(size*besig*besig*besig);
  rgbeker=rgbeker/(size*rgbesig*rgbesig*rgbesig*rgbesig);
  reker=reker/(size*resig*resig*resig*resig);
  geker=geker/(size*gesig*gesig*gesig*gesig);
  beker=beker/(size*besig*besig*besig*besig);

  rgbepnt=rgbepnt/(size);
  repnt=repnt/(size);
  gepnt=gepnt/(size);
  bepnt=bepnt/(size);
  
  rgbevec=0.0;
  revec=0.0;
  gevec=0.0;
  bevec=0.0;
  for (i=0;i<256;i++) {
    dtmp=(float)(rgbh[i]);
    if(option_sig == 0) {
      xfac=xrgbe[i];
    } else {
      xfac=dtmp;
    }
    rgbevec+=xfac*xfac;
    dtmp=(float)(rh[i]);
    if(option_sig == 0) {
      xfac=xre[i];
    } else {
      xfac=dtmp;
    }
    revec+=xfac*xfac;
    dtmp=(float)(gh[i]);
    if(option_sig == 0) {
      xfac=xge[i];
    } else {
      xfac=dtmp;
    }
    gevec+=xfac*xfac;
    dtmp=(float)(bh[i]);
    if(option_sig == 0) {
      xfac=xbe[i];
    } else {
      xfac=dtmp;
    }
    bevec+=xfac*xfac;
  }
  rgbevec=sqrt(rgbevec)/(256.0);
  revec=sqrt(revec)/(256.0);
  gevec=sqrt(gevec)/(256.0);
  bevec=sqrt(bevec)/(256.0);

  data_sig[ 0]=rgbepnt;
  data_sig[ 1]=rgbevar;
  data_sig[ 2]=rgbesku;
  data_sig[ 3]=rgbeker;
  data_sig[ 4]=rgbevec;

  data_sig[ 5]=repnt;
  data_sig[ 6]=revar;
  data_sig[ 7]=resku;
  data_sig[ 8]=reker;
  data_sig[ 9]=revec;

  data_sig[10]=gepnt;
  data_sig[11]=gevar;
  data_sig[12]=gesku;
  data_sig[13]=geker;
  data_sig[14]=gevec;

  data_sig[15]=bepnt;
  data_sig[16]=bevar;
  data_sig[17]=besku;
  data_sig[18]=beker;
  data_sig[19]=bevec;
}
Last edited on
At a glance, I see a couple of things:

* You are performing more pow/log function calls (to C library functions) in the new code;
* You are performing 64-bit arithmetic in the new code, but not in the old code.

If you are using a 32-bit OS, be aware that _all_ arithmetic operations on 64-bit types
result in function calls because 64-bit arithmetic has to be done in software rather than
hardware.

* You are performing more pow/log function calls (to C library functions) in the new code;


This

For small interger powers (like 2, 3, 4 like you're doing in your code) you're better off manually mutiplying than calling pow(). pow's strength is with abnormal powers (negative, floating point, etc).
Thanks guys.

64-bit is not a problem, but the calls to math.h definitely were.

abnormal numbers... hehe ;)
Topic archived. No new replies allowed.