Skip to content

Instantly share code, notes, and snippets.

@MaanooAk
Created May 16, 2017 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaanooAk/0cf866b197eb5cd7d51cf3c26e3a6ec3 to your computer and use it in GitHub Desktop.
Save MaanooAk/0cf866b197eb5cd7d51cf3c26e3a6ec3 to your computer and use it in GitHub Desktop.
// run with:
// g++ --std=c++14 -O3 eucl_variations.cpp && ./a.out && rm a.out
// output:
// 12139783
// 7026125
// 5667812
// 12146435
// 7126185
// 5592648
#include <iostream>
#include <stdio.h>
#include <cmath>
float inline eucl1(const float x1, const float y1, const float x2, const float y2) {
float dx = x1 - x2;
float dy = y1 - y2;
return std::hypot(dx, dy);
}
float inline eucl2(const float x1, const float y1, const float x2, const float y2) {
float dx = x1 - x2;
float dy = y1 - y2;
return std::sqrt(dx*dx + dy*dy);
}
float inline eucl3(const float x1, const float y1, const float x2, const float y2) {
float dx = x1 - x2;
float dy = y1 - y2;
return dx*dx + dy*dy;
}
float inline eucl4(const float x1, const float y1, const float x2, const float y2) {
return std::hypot(x1 - x2, y1 - y2);
}
float inline eucl5(const float x1, const float y1, const float x2, const float y2) {
return std::sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
}
float inline eucl6(const float x1, const float y1, const float x2, const float y2) {
return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
}
int main(int argc, char *argv[]) {
int n = 100000000;
int nv = 10000;
int r = 10;
float *values = new float[nv];
long time;
float tmp = 0;
for (int i=0; i<nv; i++) values[i] = rand() * 100.0 / RAND_MAX;
for (int ir=0; ir<r; ir++) for (int i=0; i<n; i++) tmp += eucl1(values[i%nv], values[(i+1)%nv], values[(i+2)%nv], values[(i+3)%nv]) > 0 ? 0 : 1;
time = clock();
for (int ir=0; ir<r; ir++) for (int i=0; i<n; i++) tmp += eucl1(values[i%nv], values[(i+1)%nv], values[(i+2)%nv], values[(i+3)%nv]) > 0 ? 0 : 1;
std::cout << clock() - time << std::endl;
time = clock();
for (int ir=0; ir<r; ir++) for (int i=0; i<n; i++) tmp += eucl2(values[i%nv], values[(i+1)%nv], values[(i+2)%nv], values[(i+3)%nv]) > 0 ? 0 : 1;
std::cout << clock() - time << std::endl;
time = clock();
for (int ir=0; ir<r; ir++) for (int i=0; i<n; i++) tmp += eucl3(values[i%nv], values[(i+1)%nv], values[(i+2)%nv], values[(i+3)%nv]) > 0 ? 0 : 1;
std::cout << clock() - time << std::endl;
time = clock();
for (int ir=0; ir<r; ir++) for (int i=0; i<n; i++) tmp += eucl4(values[i%nv], values[(i+1)%nv], values[(i+2)%nv], values[(i+3)%nv]) > 0 ? 0 : 1;
std::cout << clock() - time << std::endl;
time = clock();
for (int ir=0; ir<r; ir++) for (int i=0; i<n; i++) tmp += eucl5(values[i%nv], values[(i+1)%nv], values[(i+2)%nv], values[(i+3)%nv]) > 0 ? 0 : 1;
std::cout << clock() - time << std::endl;
time = clock();
for (int ir=0; ir<r; ir++) for (int i=0; i<n; i++) tmp += eucl6(values[i%nv], values[(i+1)%nv], values[(i+2)%nv], values[(i+3)%nv]) > 0 ? 0 : 1;
std::cout << clock() - time << std::endl;
for (int ir=0; ir<r; ir++) for (int i=0; i<n; i++) tmp += eucl1(values[i%nv], values[(i+1)%nv], values[(i+2)%nv], values[(i+3)%nv]) > 0 ? 0 : 1;
return tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment