Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Float square vs abs vs sqrt
#!/bin/bash
set -e
for opt in -O0 -O1 -O2 -O3; do
echo "GCC PARAM: $opt"
gcc -c -o libtest.o libtest.c "$opt"
gcc -o main.exe main.c libtest.o -lm
./main.exe
echo ""
done
#include <math.h>
double l2_loss(const double *arr1, const double *arr2, int n) {
double sum = 0.0;
for (int i=0; i<n; i++) {
double diff = arr2[i] - arr1[i];
sum += diff * diff;
}
return sqrt(sum);
}
double l1_loss(const double *arr1, const double *arr2, int n) {
double sum = 0.0;
for (int i=0; i<n; i++) {
double diff = arr2[i] - arr1[i];
sum += fabs(diff);
}
return sum;
}
double l05_loss(const double *arr1, const double *arr2, int n) {
double sum = 0.0;
for (int i=0; i<n; i++) {
double diff = arr2[i] - arr1[i];
sum += sqrt(fabs(diff));
}
return sum * sum;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_SIZE 1000000000
double l2_loss(const double *arr1, const double *arr2, int n);
double l1_loss(const double *arr1, const double *arr2, int n);
double l05_loss(const double *arr1, const double *arr2, int n);
int main(void) {
double *arr1, *arr2, result;
clock_t t0, t1;
arr1 = malloc(ARR_SIZE * sizeof(double));
arr2 = malloc(ARR_SIZE * sizeof(double));
srand(time(NULL));
for (int i = 0; i < ARR_SIZE; i++) {
arr1[i] = rand() / (double)RAND_MAX;
arr2[i] = rand() / (double)RAND_MAX;
}
t0 = clock();
result = l2_loss(arr1, arr2, ARR_SIZE);
t1 = clock();
printf("L2 LOSS: %lg\n", result);
printf("CLOCK: %Lf\n", (long double)(t1 - t0));
t0 = clock();
result = l1_loss(arr1, arr2, ARR_SIZE);
t1 = clock();
printf("L1 LOSS: %lg\n", result);
printf("CLOCK: %Lf\n", (long double)(t1 - t0));
t0 = clock();
result = l05_loss(arr1, arr2, ARR_SIZE);
t1 = clock();
printf("L0.5 LOSS: %lg\n", result);
printf("CLOCK: %Lf\n", (long double)(t1 - t0));
free(arr1);
free(arr2);
return 0;
}
@cuihaoleo
Copy link
Author

cuihaoleo commented Jun 10, 2022

gcc 12.1.0 + Core i7-10700K:

GCC PARAM: -O0
L2 LOSS: 12910
CLOCK: 1908835.000000
L1 LOSS: 3.33333e+08
CLOCK: 1907601.000000
L0.5 LOSS: 2.84442e+17
CLOCK: 2543787.000000

GCC PARAM: -O1
L2 LOSS: 12909.7
CLOCK: 943745.000000
L1 LOSS: 3.33328e+08
CLOCK: 945035.000000
L0.5 LOSS: 2.84441e+17
CLOCK: 1905580.000000

GCC PARAM: -O2
L2 LOSS: 12909.7
CLOCK: 936801.000000
L1 LOSS: 3.33329e+08
CLOCK: 938949.000000
L0.5 LOSS: 2.84441e+17
CLOCK: 1323443.000000

GCC PARAM: -O3
L2 LOSS: 12909.8
CLOCK: 921332.000000
L1 LOSS: 3.33334e+08
CLOCK: 921441.000000
L0.5 LOSS: 2.84447e+17
CLOCK: 1364293.000000

@cuihaoleo
Copy link
Author

cuihaoleo commented Jun 10, 2022

gcc 10.2.1 + Opteron 6180 SE:

GCC PARAM: -O0
L2 LOSS: 12910
CLOCK: 10587803.000000
L1 LOSS: 3.33337e+08
CLOCK: 10846058.000000
L0.5 LOSS: 2.84448e+17
CLOCK: 14458580.000000

GCC PARAM: -O1
L2 LOSS: 12909.6
CLOCK: 2566798.000000
L1 LOSS: 3.3332e+08
CLOCK: 2562385.000000
L0.5 LOSS: 2.84429e+17
CLOCK: 10450719.000000

GCC PARAM: -O2
L2 LOSS: 12910.4
CLOCK: 2773337.000000
L1 LOSS: 3.33348e+08
CLOCK: 2760192.000000
L0.5 LOSS: 2.84457e+17
CLOCK: 10059200.000000

GCC PARAM: -O3
L2 LOSS: 12910.1
CLOCK: 2513108.000000
L1 LOSS: 3.33342e+08
CLOCK: 2493310.000000
L0.5 LOSS: 2.84456e+17
CLOCK: 10057137.00000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment