News for hackers. Stuff that matters.
Re:Why is this C++ code faster than my hand-written assembly... by gnaritas on Tuesday August 22, 2017 @12:39PM (#15074070)
> but sometimes it's nice to make a habit of doing things the faster way when you have two choices that are otherwise indistinguishable.

That is virtually never the case.

> For example I try to use ++x instead of x++

Those have wildly different semantics.

Re:Why is this C++ code faster than my hand-written assembly... by colejohnson66 on Tuesday August 22, 2017 @01:09PM (#15074371)
Yes, but if used as their own statement and not used as some bigger expression (parameter to function, etc.), any decent compiler will compile them the same way.
Re:Why is this C++ code faster than my hand-written assembly... by gnaritas on Tuesday August 22, 2017 @01:12PM (#15074406)
Yes, and thus a perfect example premature optimization; semantics should define default usage, not some possible micro optimization. This is exactly the kind of case Knuth is talking about, going around doing ++x because you think it's faster when the standard idiom is x++ is premature optimization.
Re:Why is this C++ code faster than my hand-written assembly... by zerr on Tuesday August 22, 2017 @01:32PM (#15074618)
x++ is not the "standard idiom".
Re:Why is this C++ code faster than my hand-written assembly... by mark-r on Tuesday August 22, 2017 @02:23PM (#15075097)
In my experience x++ is far more common than ++x when it doesn't matter which you use, "for" statements for example.
Re:Why is this C++ code faster than my hand-written assembly... by gnaritas on Wednesday August 23, 2017 @01:51PM (#15083574)
Yes, it is.
Re:Why is this C++ code faster than my hand-written assembly... by zerr on Thursday August 24, 2017 @01:11AM (#15087748)
I meant no more than ++x.
Re:Why is this C++ code faster than my hand-written assembly... by gnaritas on Thursday August 24, 2017 @10:32AM (#15090389)
I know what you meant, I don't agree. Scour any large codebase, x++ is far more idiomatic than ++x.
Re:Why is this C++ code faster than my hand-written assembly... by zerr on Friday August 25, 2017 @03:41PM (#15101563)
What about top C++ books? Also when it comes to advancing iterators...
Re:Why is this C++ code faster than my hand-written assembly... by marcosdumay on Tuesday August 22, 2017 @01:32PM (#15074611)
Well, there is no reason for any standard idiom there.

Those `++` and `--` operators are something to avoid anyway.

Re:Why is this C++ code faster than my hand-written assembly... by jcelerier on Tuesday August 22, 2017 @02:44PM (#15075272)
Notice how the asm changes here if changing it++ to ++it: https://godbolt.org/g/W5w7Dc
Re:Why is this C++ code faster than my hand-written assembly... by Karliss on Tuesday August 22, 2017 @03:36PM (#15075665)
Looks like only thing that changes is label name which doesn't matter.
Re:Why is this C++ code faster than my hand-written assembly... by jcelerier on Wednesday August 23, 2017 @03:01AM (#15079190)
nope, there are less instructions
Re:Why is this C++ code faster than my hand-written assembly... by vardump on Wednesday August 23, 2017 @09:37AM (#15081400)
Except that those instructions are not actually called in either case from function foo(). So they're dead code and linker will remove them.