Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

3 milliseconds, man, the C optimizers blow my mind, they basically just cheat and stick the answer in there. :-)

[C]cat sum.c #include <stdio.h> int main(void) { unsigned long long sum = 0, i; for (i = 0; i <= 1000000000; i++) //one billion sum += i; printf("%lld\n", sum); //500000000500000000 return 0; }

[C]time ./a.out 500000000500000000

real 0m0.003s user 0m0.001s sys 0m0.001s [C]gcc -O3 -S sum.c [C]cat sum.s .section __TEXT,__text,regular,pure_instructions .globl _main .align 4, 0x90 _main: Leh_func_begin1: pushq %rbp Ltmp0: movq %rsp, %rbp Ltmp1: leaq L_.str(%rip), %rdi movabsq $500000000500000000, %rsi xorb %al, %al callq _printf xorl %eax, %eax popq %rbp ret



Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: