Created
July 12, 2016 20:58
-
-
Save abatilo/90b01aafffd9493bcd17b21ab429ebd8 to your computer and use it in GitHub Desktop.
Timing different abs implementations for Aaron
This file contains 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 <time.h> | |
#include <stdlib.h> | |
#include <math.h> | |
double absd(double n) { | |
unsigned long *gg = (unsigned long int *)&n; | |
*(gg) &= 0x7FFFFFFFFFFFFFFFull; | |
return n; | |
} | |
int main() { | |
// Random seed | |
srand(0); | |
double sum = 0; | |
// Generate 2,000,000,000 random numbers | |
for (int i = 0; i < 2000000000; ++i) { | |
// sum += absd((double) (rand() - RAND_MAX) / RAND_MAX); | |
sum += fabs((double) (rand() - RAND_MAX) / RAND_MAX); | |
} | |
printf("%f\n", sum); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment