How Can I Improve/SpeedUp This FrequentFunction in C?
Posted
by Peter Lee
on Stack Overflow
See other posts from Stack Overflow
or by Peter Lee
Published on 2010-04-19T22:16:46Z
Indexed on
2010/04/19
22:23 UTC
Read the original article
Hit count: 288
efficiency
|c
Hi folks,
How can I improve / speed up this frequent function?
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Assumptions: x, y, z, a, b and c are all array of 10.
// Requirement: return the value of ret;
// get all elements of array c
float fnFrequentFunction(const float* x, const float* y, const float* z,
const float *a, const float *b, float *c, int M)
{
register float tmp;
register float sum;
register float ret = 0;
int i;
for (i = 0; i < M; i++) // M == 1, 2, 4, or 8
{
sum = 0;
tmp = x[0] - y[0]; sum += tmp * tmp * z[0];
tmp = x[1] - y[1]; sum += tmp * tmp * z[1];
tmp = x[2] - y[2]; sum += tmp * tmp * z[2];
tmp = x[3] - y[3]; sum += tmp * tmp * z[3];
tmp = x[4] - y[4]; sum += tmp * tmp * z[4];
tmp = x[5] - y[5]; sum += tmp * tmp * z[5];
tmp = x[6] - y[6]; sum += tmp * tmp * z[6];
tmp = x[7] - y[7]; sum += tmp * tmp * z[7];
tmp = x[8] - y[8]; sum += tmp * tmp * z[8];
tmp = x[9] - y[9]; sum += tmp * tmp * z[9];
ret += (c[i] = log(a[i] * b[i]) + sum);
}
return ret;
}
int main()
{
float x[10] = {0.001251f, 0.563585f, 0.193304f, 0.808741f, 0.585009f, 0.479873f, 0.350291f, 0.895962f, 0.622840f, 0.746605f};
float y[10] = {0.864406f, 0.709006f, 0.091433f, 0.995727f, 0.227180f, 0.902585f, 0.659047f, 0.865627f, 0.846767f, 0.514359f};
float z[10] = {0.866817f, 0.581347f, 0.175542f, 0.620197f, 0.781823f, 0.778588f, 0.938688f, 0.721610f, 0.940214f, 0.811353f};
float a[10] = {0.870205f, 0.733879f, 0.711386f, 0.588244f, 0.484176f, 0.852962f, 0.168126f, 0.684286f, 0.072573f, 0.632160f};
float b[10] = {0.871487f, 0.998108f, 0.798608f, 0.134831f, 0.576281f, 0.410779f, 0.402936f, 0.522935f, 0.623218f, 0.193030f};
float c[8];
int i;
int n = 10000000;
long start;
// Speed test here:
start = clock();
while(--n)
fnFrequentFunction(x, y, z, a, b, c, 8);
printf("Time used: %ld\n", clock() - start);
printf("fnFrequentFunction == %f\n", fnFrequentFunction(x, y, z, a, b, c, 8));
for(i = 0; i < 8; ++i)
printf(" c[%d] == %f\n", i, c[i]);
printf("\n");
return 0;
}
Any suggestions are welcome :-)
© Stack Overflow or respective owner