Created
May 30, 2018 15:35
-
-
Save guillem/2d502e7475ebcfc05311a22cd0263d33 to your computer and use it in GitHub Desktop.
Why Go takes 30 seconds and C just 0.2 ?
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> | |
#define MIN 1 | |
#define MAX 1000001 | |
int main (int argc, char **argv) { | |
for (int secret = MIN; secret <= MAX-1; secret++) { | |
printf("%d", secret); | |
int min = MIN; | |
int max = MAX; | |
while(1) { | |
int guess = (min + max) / 2; | |
if (guess < secret) { | |
printf("<"); | |
min = guess; | |
} else if (guess > secret) { | |
printf(">"); | |
max = guess; | |
} else { | |
printf(".\n"); | |
break; | |
} | |
} | |
} | |
} |
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
package main | |
import "fmt" | |
func main() { | |
const MIN = 1 | |
const MAX = 1000001 | |
for secret := MIN; secret <= MAX-1; secret++ { | |
fmt.Printf("%d", secret) | |
min := MIN | |
max := MAX | |
for { | |
guess := (min + max) / 2 | |
if guess < secret { | |
fmt.Printf("<") | |
min = guess | |
} else if guess > secret { | |
fmt.Printf(">") | |
max = guess | |
} else { | |
fmt.Printf(".\n") | |
break | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment