Skip to content

Instantly share code, notes, and snippets.

@guillem
Created May 30, 2018 15:35
Show Gist options
  • Save guillem/2d502e7475ebcfc05311a22cd0263d33 to your computer and use it in GitHub Desktop.
Save guillem/2d502e7475ebcfc05311a22cd0263d33 to your computer and use it in GitHub Desktop.
Why Go takes 30 seconds and C just 0.2 ?
#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;
}
}
}
}
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