dist_v1/main.cpp
#include <math.h>
#include "stdlib.h"
#define N 64
float scale(int i, int n)
{
return ((float)i / (n - 1));
}
float distance(float x1, float x2)
{
return sqrt((x2 - x1)*(x2 - x1));
}
int main()
{
float out[N] = { 0.0f };
const float ref = 0.5f;
for (int i =0;i<N;i++)
{
float x = scale(i, N);
out[i] = distance(x, ref);
}
system("pause");
return 0;
}
dist_v2/main.cpp
#include "aux_function.h"
#include <stdlib.h>
#define N 20000000
int main()
{
// float in[N] = { 0.0f };
// float out[N] = { 0.0f };
float *in = (float*)calloc(N, sizeof(float));
float *out = (float*)calloc(N, sizeof(float));
const float ref = 0.5f;
for (int i =0; i<N; i++)
{
in[i] = scale(i, N);
}
distanceArray(out, in, ref, N);
free(in);
free(out);
return 0;
}
dist_v2/aux_function.h
#pragma once
float scale(int i, int n);
float distance(float x1, float x2);
void distanceArray(float *out, float *in, float ref, int N);
dist_v2/aux_function.cpp
#include "aux_function.h"
#include <math.h>
float scale(int i, int n)
{
return ((float)i / (n - 1));
}
float distance(float x1, float x2)
{
//return sqrt((x2-x1)*(x2 - x1));
return fabsf(x1 - x2);
}
void distanceArray(float *out, float *in, float ref, int n)
{
for (int i = 0; i<n ; i++)
{
out[i] =distance(in[i],ref) ;
}
}