Created
June 10, 2022 20:00
-
-
Save cuihaoleo/e51473e0b1b17dfef26ee25591b7ad66 to your computer and use it in GitHub Desktop.
Float square vs abs vs sqrt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
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
gcc 12.1.0 + Core i7-10700K: