Article View: comp.lang.c++
Article #973Re: What does this code
From: David Brown
Date: Sat, 21 Nov 2026 13:55
Date: Sat, 21 Nov 2026 13:55
41 lines
3499 bytes
3499 bytes
On 21/11/2026 13:10, Bonita Montero wrote: > Am 21.11.2026 um 12:38 schrieb David Brown: > >> That is very different code, and the compiler has different heuristics >> for generating object code. ... > > It doesn't fit with your statement. > > > That's the kind of hack "manual optimisation" code that most C and > > C++ programmers were able to leave behind at the turn of the century > > as compilers got better at optimising well-written and clear source > > code. > > If I did it more simple and readable from the beginning the code > wouldn't be as efficient - as you suggested. > I think the branchless code will often be more efficient in practice when the data is already in registers (but see Michael's comments about the code - and he is quite experienced in that kind of thing). When the data is in memory, not a register, I expect the branched code is better. gcc generates the best object code - if my predictions are correct - from the clearest source code, both in your struct method and for the stand-alone function example. In real usage, methods like this would be inlined, and the code generated would depend on the surrounding source code. In general, clearer source code gives the compiler better opportunities to optimise the inlined code well. The code you wrote is not "cool" or "smart" - it is a smart-arse hack that results in significantly poorer source code quality, and worse object code efficiency (though it probably makes no measurable difference in real-world usage). Before writing such ugly code, you should be benchmarking the results of clear source code in real usage, and determining if there is actually a bottleneck in your code performance caused by the function. Then after writing your hack, you should test again and determine if there is a significant enough gain for it to be worth keeping. I do not believe you have made the slightest attempt to do that.
Technical Details
Message-ID:
<11ds7s0$3uj9$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11dou16$2vlr8$1@raubtier-asyl.eternal-september.org> <20261120114427.00002e24@yahoo.com> <11dp7lk$330qr$1@dont-email.me> <11dp9fv$33khk$1@raubtier-asyl.eternal-september.org> <11dpbgr$34a3s$1@dont-email.me> <11draqa$3qusi$1@raubtier-asyl.eternal-september.org> <11drvr5$12fg$2@dont-email.me> <11ds1o6$20l4$1@raubtier-asyl.eternal-september.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c++ Subject: Re: What does this code Date: Sat, 21 Nov 2026 14:55:12 +0100 Organization: A noiseless patient Spider Lines: 41 Message-ID: <11ds7s0$3uj9$1@dont-email.me> References: <11dou16$2vlr8$1@raubtier-asyl.eternal-september.org> <20261120114427.00002e24@yahoo.com> <11dp7lk$330qr$1@dont-email.me> <11dp9fv$33khk$1@raubtier-asyl.eternal-september.org> <11dpbgr$34a3s$1@dont-email.me> <11draqa$3qusi$1@raubtier-asyl.eternal-september.org> <11drvr5$12fg$2@dont-email.me> <11ds1o6$20l4$1@raubtier-asyl.eternal-september.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 21 Nov 2026 13:55:13 +0000 (UTC) Injection-Info: dont-email.me; logging-data="129641"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19HjKK4cVzR3OLFa1KkYypaUxkEdmJCDdM="; posting-host="e9cc8568760c122c84a8bb7d362e291d" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:Kp5canSHZuhsX6k+G/cfFGy6eMM= sha256:2x4yN+AvEakuo3DtreddDM00Z8q9mIcZAHHD/lxPBm0= sha1:kSiToq3UaZuEm7CQafHXH+O4hg8= sha256:iNvG7YsYSC+VmDqrA9jV4AVDEqrYLK+6/rTfyVIJsIo= Content-Language: en-GB In-Reply-To: <11ds1o6$20l4$1@raubtier-asyl.eternal-september.org>