4 ms·
GCC with -O3 might try to unroll this loop into a single constant. O(0) is pretty fast.
by gravypod 10y ago
GCC with -O3 might try to unroll this loop into a single constant. O(0) is pretty fast.
- supergarfield 10y agoThat's very likely. Clang 8.0 collapses the loop with any optimizer setting other than -O0. But if you had to do it manually, loop unrolling and SIMD instructions (although not part of standard C) would be good bets and can probably get you an order of magnitude.
- dom0 10y agoGCC as expected, O>2 sub rsp, 8 mov rdi, QWORD PTR [rsi+8] mov edx, 10 xor esi, esi call strtol <-- atoi xor eax, eax <-- zero return value add rsp, 8 ret <-- return from main Interestingly enough it does not elide the entire function body.
- gravypod 10y agoHow do you get objdump -S to print out non AT&T syntax? My output looked like this: 000000000400400 <main>: 400400: 48 83 ec 08 sub $0x8,%rsp 400404: 48 8b 7e 08 mov 0x8(%rsi),%rdi 400408: ba 0a 00 00 00 mov $0xa,%edx 40040d: 31 f6 xor %esi,%esi 40040f: e8 dc ff ff ff callq 4003f0 <strtol@plt> 400414: 31 c0 xor %eax,%eax 400416: 48 83 c4 08 add $0x8,%rsp 40041a: c3 retq 40041b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) Which I'm not a fan of. I've always wanted "add (store), (get)" but could never get that.
- dom0 10y agoNot on a development machine right now, so I just went to godbolt ;) https://godbolt.org/ https://godbolt.org/
- userbinator 10y agoUse the '-M intel' option.
- gens 10y ago-M intel
- deleted 10y ago[deleted]
- jwilk 10y agoITYM "O(1)". "O(0)" is pretty much meaningless.
- gravypod 10y agoI mean O(0) because no operation occured. The only operation that will occur is parsing the data which is not the problem specified. When looking at the loop, and just the loop, O(0) is the correct answer for this.