Created
March 6, 2012 20:09
-
-
Save jkubicek/1988702 to your computer and use it in GitHub Desktop.
FizzBuzz
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 <sys/time.h> | |
#import <Foundation/Foundation.h> | |
int | |
timeval_subtract (result, x, y) | |
struct timeval *result, *x, *y; | |
{ | |
/* Perform the carry for the later subtraction by updating y. */ | |
if (x->tv_usec < y->tv_usec) { | |
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; | |
y->tv_usec -= 1000000 * nsec; | |
y->tv_sec += nsec; | |
} | |
if (x->tv_usec - y->tv_usec > 1000000) { | |
int nsec = (x->tv_usec - y->tv_usec) / 1000000; | |
y->tv_usec += 1000000 * nsec; | |
y->tv_sec -= nsec; | |
} | |
/* Compute the time remaining to wait. | |
tv_usec is certainly positive. */ | |
result->tv_sec = x->tv_sec - y->tv_sec; | |
result->tv_usec = x->tv_usec - y->tv_usec; | |
/* Return 1 if result is negative. */ | |
return x->tv_sec < y->tv_sec; | |
} | |
static const long kfizz = 0; | |
static const long kbuzz = -1; | |
static const long kfizzbuzz = -2; | |
//#define printDatShit printf | |
#define printDatShit | |
int main(int argc, const char * argv[]) | |
{ | |
long n = 500000000; | |
long *fizzbuzz = malloc(n*sizeof(long)); | |
for (long i = 0; i<n; i++) { | |
long value = i+1; | |
if (value % 15 == 0) | |
fizzbuzz[i] = kfizzbuzz; | |
else if(value % 3 == 0) | |
fizzbuzz[i] = kfizz; | |
else if( value % 5 == 0) | |
fizzbuzz[i] = kbuzz; | |
else | |
fizzbuzz[i] = value; | |
} | |
struct timeval start, end, diff; | |
gettimeofday(&start, NULL); | |
for (long i = 0; i<n; i++) { | |
long value = fizzbuzz[i]; | |
if (value == kfizz) | |
printDatShit("fizz\n"); | |
else if (value == kbuzz) | |
printDatShit("buzz\n"); | |
else if (value == kfizzbuzz) | |
printDatShit("fizzbuzz\n"); | |
else | |
printDatShit("%li\n", value); | |
} | |
gettimeofday(&end, NULL); | |
timeval_subtract(&diff, &end, &start); | |
printf("fizzbuzz1 time=%ld.%06d seconds\n", diff.tv_sec, diff.tv_usec); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment