4 ms·
Pointless and bad article. I compiled the presented code out curiosity on arm gcc 8.2 on godbolt.org with -O3 op and the results are: (the original remove is
by andy_threos_io 6y ago
Pointless and bad article.
I compiled the presented code out curiosity on arm gcc 8.2 on godbolt.org with -O3 op and the results are:
(the original remove is even faster then the so called elegant or the elegant with inline)
remove_cs101:
ldr r2, [r0]
cmp r2, r1
bne .L3
b .L9
.L6:
mov r2, r3
.L3:
ldr r3, [r2, #4]
cmp r1, r3
bne .L6
ldr r3, [r1, #4]
str r3, [r2, #4]
bx lr
.L9:
ldr r3, [r2, #4]
str r3, [r0]
bx lr
remove_elegant:
ldr r2, [r0]
cmp r1, r2
bne .L12
b .L13
.L14:
mov r2, r3
.L12:
ldr r3, [r2, #4]
cmp r1, r3
bne .L14
add r0, r2, #4
.L13:
ldr r3, [r1, #4]
str r3, [r0]
bx lr
remove_elegant_with_inline:
ldr r2, [r0]
cmp r1, r2
cmpne r2, #0
bne .L17
b .L18
.L19:
mov r2, r3
.L17:
ldr r3, [r2, #4]
cmp r1, r3
cmpne r3, #0
bne .L19
add r0, r2, #4
.L18:
ldr r3, [r1, #4]
str r3, [r0]
bx lr
EDIT: Hmmm, so much downvote without any reply.
Well if your code needs an article to be explained, rather than the plain solution and brings nothing, even 1 instruction slower, as the compiler can't make the same (faster) result as the plain solution, than your code is pointless. And stating that this is the elegant solution is IMO bad practice.
- sltkr 6y agoFirst, you haven't demonstrated at all that the "elegant" solution is slower. Between `remove_cs101` and `remove_elegant`, it looks like they are equivalent (in particular, the inner loop has the same number of instructions) except the former uses more instructions. Second, I think you're missing the point by focusing on the code generated by the compiler. Clearly the two algorithms are equivalent, and a good compiler can generate equivalent code for them, up to an instruction more or less. The bigger concern here is about the source code: how can we express the algorithm in the most elegant (think: clear, concise, and correct) way? The exact generated bytecode is irrelevant, so long as the compiler is able to generate something reasonable, which is clearly the case here, as you've demonstrated.
- andy_threos_io 6y agoI don't see the so called "elegant" solution any special at all, but if the level 1 programmer in your org needs more time to understand, and maybe making bugs or mistakes, coz of not fully understand the code, than it's a problem. See Occam razor. The "elegant" solution tries to be smart, or optimize where there is no need at all. Yes the simple "elegant" solution is just 1 instruction slower, and longer only in the end, but the inline version is 1 instruction slower in the loop, which is stated more elegant in the article. EDIT: Or you can get the same principle as the Occam's razor, which is KISS keep it simple, stupid, silly and straightforward
- scscsc 6y agoThe key here is "it looks like they are equivalent". In fact, I am not sure they are equivalent (in general, without taking into account the context), and even if they were, proving this would be S.F. for a compiler today (2020). Even coming up with such an optimisation without hardcoding it does not seem plausible.
- scscsc 6y agoYou are probably right regarding speed, but the article is definitely not pointless and bad.