Skip to content

Instantly share code, notes, and snippets.

@brannondorsey
Last active November 8, 2022 08:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brannondorsey/7462ae795cb11d32b480429182aff9f6 to your computer and use it in GitHub Desktop.
Save brannondorsey/7462ae795cb11d32b480429182aff9f6 to your computer and use it in GitHub Desktop.
Mean Squared Error

A comparison of the Mean Squared Error algorithm in C, Python, and JavaScript.

C

gcc mse.c -O3 -o mse -lm
time ./mse
348.172699

real	0m0.002s
user	0m0.000s
sys	0m0.000s

Python

time python mse.py
348.1727

real	0m0.034s
user	0m0.028s
sys	0m0.004s

JavaScript

time node mse.js
348.1727

real	0m0.041s
user	0m0.040s
sys	0m0.004s
#include <stdio.h>
#include <math.h>
float mse(float a[], float b[], int size) {
float error = 0;
for (int i = 0; i < size; i++) {
error += pow((b[i] - a[i]), 2);
}
return error / size;
}
int main() {
float a[] = {1.0, 2.9, 2.8};
float b[] = {-19.8, -18.2, -10.11};
printf("%f\n", mse(a, b, 3));
}
function mse(a, b) {
let error = 0
for (let i = 0; i < a.length; i++) {
error += Math.pow((b[i] - a[i]), 2)
}
return error / a.length
}
a = [1.0, 2.9, 2.8]
b = [-19.8, -18.2, -10.11]
console.log(mse(a, b))
def mse(a, b):
error = 0.0
for i in range(len(a)):
error += (b[i] - a[i]) ** 2
return error / len(a)
a = [1.0, 2.9, 2.8]
b = [-19.8, -18.2, -10.11]
print(mse(a, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment