Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #79327 > unrolled thread
| Started by | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| First post | 2021-04-27 12:07 +0000 |
| Last post | 2021-05-04 10:53 +0000 |
| Articles | 20 on this page of 40 — 13 participants |
Back to article view | Back to comp.lang.c++
C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-27 12:07 +0000
Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-04-27 17:57 +0300
Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-04-27 18:14 -0700
Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-28 05:21 +0000
Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-04-28 09:09 -0700
Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-29 04:45 +0000
Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-04-29 15:16 -0700
Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-30 10:10 +0000
Re: C++ may sometimes be *too* simple (to use) Bo Persson <bo@bo-persson.se> - 2021-04-30 15:33 +0200
Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-04 10:32 +0000
Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-01 06:51 -0700
Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-03 21:38 +0300
Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-06 18:21 -0700
Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-07 12:05 +0300
Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-04 10:43 +0000
Re: C++ may sometimes be *too* simple (to use) David Brown <david.brown@hesbynett.no> - 2021-05-04 15:20 +0200
Re: C++ may sometimes be *too* simple (to use) Manfred <noname@add.invalid> - 2021-05-04 16:41 +0200
Re: C++ may sometimes be *too* simple (to use) James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-05-04 23:42 -0400
Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-05 05:01 +0000
Re: C++ may sometimes be *too* simple (to use) David Brown <david.brown@hesbynett.no> - 2021-05-05 11:26 +0200
Re: C++ may sometimes be *too* simple (to use) James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-05-05 09:53 -0400
Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-06 21:40 -0700
Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-07 07:38 +0000
Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-07 22:32 +0300
Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-08 03:27 -0700
Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-07 12:10 +0300
Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-07 09:40 -0700
Re: C++ may sometimes be *too* simple (to use) red floyd <no.spam.here@its.invalid> - 2021-05-07 17:59 -0700
Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-08 03:42 -0700
Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-08 20:27 +0000
Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-09 09:56 -0700
Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-10 05:07 +0000
Re: C++ may sometimes be *too* simple (to use) Richard Damon <Richard@Damon-Family.org> - 2021-05-10 07:29 -0400
Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-10 20:18 -0700
Re: C++ may sometimes be *too* simple (to use) Hans Bos <hans.bos@xelion.nl> - 2021-05-07 16:06 +0200
Re: C++ may sometimes be *too* simple (to use) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-27 19:57 -0700
Re: C++ may sometimes be *too* simple (to use) wij <wyniijj@gmail.com> - 2021-04-27 21:15 -0700
Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-04-28 11:13 +0300
Re: C++ may sometimes be *too* simple (to use) "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2021-05-03 10:28 +0200
Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-04 10:53 +0000
Page 1 of 2 [1] 2 Next page →
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2021-04-27 12:07 +0000 |
| Subject | C++ may sometimes be *too* simple (to use) |
| Message-ID | <s68upq$11to$1@gioia.aioe.org> |
Recently I posted a controversial thread where I recounted my recent
experience trying to tutor a beginner programmer who wanted to create
a small program that would read a file and allow the user to edit it,
like a simple text editor, and how frustrating it was trying to do
this in C due to the complexities of its memory management.
Because such accusations about C is heresy, I thought I would redeem
myself and cleanse my sins by commenting a bit on the flipside, based
on actual examples out there that I have had to deal with.
While trying to tutor that beginner, I was constantly thinking how
much easier the task would have been in C++, using std::string
(and std::vector, etc).
Indeed, std::string makes such things so much easier and simpler.
On the flipside, perhaps it makes things a bit *too* easy, as it
induces people to create inefficient code with it, perhaps due to
ignorance or laziness.
For example, I am currently dealing with C++ code that does a lot of
things like:
if(str1.substr(n, 5) == str2)
Mind you, this is in commercial production code done by a relatively
experienced C++ programmer, not some noob.
I'm constantly thinking like "sheesh, there's already a function that
does that exact comparison in-place, without creating any temporary
copies, avoiding a useless allocation, copying of data and deallocation."
Namely:
if(str1.compare(n, 5, str2) == 0)
In this particular case it's not like the more efficient version is more
complicated to write or use, or would require several lines of code,
or something.
There are lots and lots of other examples, related to std::string
(and std::wstring), such as using it for fixed-sized strings in situations
where a small inbuilt char array would suffice, always taking a std::string
as parameter when a const char* would suffice (or could well be
provided as a more efficient alternative) and so on and so forth.
std::string is an awesome tool that makes one's life a thousand times
easier, but any competent C++ programmer should learn how to use it
*efficiently*, not just lazily. Know your tools. Use them efficiently.
[toc] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2021-04-27 17:57 +0300 |
| Message-ID | <s698nu$4tf$1@dont-email.me> |
| In reply to | #79327 |
27.04.2021 15:07 Juha Nieminen kirjutas: > Recently I posted a controversial thread where I recounted my recent > experience trying to tutor a beginner programmer who wanted to create > a small program that would read a file and allow the user to edit it, > like a simple text editor, and how frustrating it was trying to do > this in C due to the complexities of its memory management. > > Because such accusations about C is heresy, I thought I would redeem > myself and cleanse my sins by commenting a bit on the flipside, based > on actual examples out there that I have had to deal with. > > While trying to tutor that beginner, I was constantly thinking how > much easier the task would have been in C++, using std::string > (and std::vector, etc). > > Indeed, std::string makes such things so much easier and simpler. > On the flipside, perhaps it makes things a bit *too* easy, as it > induces people to create inefficient code with it, perhaps due to > ignorance or laziness. > > For example, I am currently dealing with C++ code that does a lot of > things like: > > if(str1.substr(n, 5) == str2) > > Mind you, this is in commercial production code done by a relatively > experienced C++ programmer, not some noob. > > I'm constantly thinking like "sheesh, there's already a function that > does that exact comparison in-place, without creating any temporary > copies, avoiding a useless allocation, copying of data and deallocation." > Namely: > > if(str1.compare(n, 5, str2) == 0) I have wondered about this exact thing myself. So I now made a little test to check if and how much the slowdown actually is. It looks like with small strings so that SSO gets used, substr() comparing is ca 2x slower than compare(), and with large strings (needing a dynamic allocation) substr() comparing is ca 10x slower than compare(). OTOH, a single compare() is only something like 4 ns, so if a program makes e.g. a million of such comparisons, then with large substrings it would slow down from 0.004 s to 0.04 s. I bet many people won't be overly concerned about this.
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2021-04-27 18:14 -0700 |
| Message-ID | <85cd53e7-a0c5-435b-9d6a-586e628c0934n@googlegroups.com> |
| In reply to | #79329 |
On Tuesday, 27 April 2021 at 17:57:17 UTC+3, Paavo Helde wrote: > 27.04.2021 15:07 Juha Nieminen kirjutas: > > Recently I posted a controversial thread where I recounted my recent > > experience trying to tutor a beginner programmer who wanted to create > > a small program that would read a file and allow the user to edit it, > > like a simple text editor, and how frustrating it was trying to do > > this in C due to the complexities of its memory management. > > > > Because such accusations about C is heresy, I thought I would redeem > > myself and cleanse my sins by commenting a bit on the flipside, based > > on actual examples out there that I have had to deal with. > > > > While trying to tutor that beginner, I was constantly thinking how > > much easier the task would have been in C++, using std::string > > (and std::vector, etc). > > > > Indeed, std::string makes such things so much easier and simpler. > > On the flipside, perhaps it makes things a bit *too* easy, as it > > induces people to create inefficient code with it, perhaps due to > > ignorance or laziness. > > > > For example, I am currently dealing with C++ code that does a lot of > > things like: > > > > if(str1.substr(n, 5) == str2) > > > > Mind you, this is in commercial production code done by a relatively > > experienced C++ programmer, not some noob. > > > > I'm constantly thinking like "sheesh, there's already a function that > > does that exact comparison in-place, without creating any temporary > > copies, avoiding a useless allocation, copying of data and deallocation." > > Namely: > > > > if(str1.compare(n, 5, str2) == 0) > I have wondered about this exact thing myself. So I now made a little > test to check if and how much the slowdown actually is. > > It looks like with small strings so that SSO gets used, substr() > comparing is ca 2x slower than compare(), and with large strings > (needing a dynamic allocation) substr() comparing is ca 10x slower than > compare(). > > OTOH, a single compare() is only something like 4 ns, so if a program > makes e.g. a million of such comparisons, then with large substrings it > would slow down from 0.004 s to 0.04 s. I bet many people won't be > overly concerned about this. The focus has long gone away from considering each time we solve some problem if the result is most efficient. On most cases it matters if it works correctly for all input and if it is easy to understand what is going on. Efficiency matters only for about 5% of code base. For example if we need to sort very small array of fixed size then on 95% of cases it is not worth to dig out some code that implements one of such mathematically proven optimal from Donald Knuth's "The Art of Computer Programming" volume 3 (written in sixties) but to use std::sort.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2021-04-28 05:21 +0000 |
| Message-ID | <s6arcp$shk$1@gioia.aioe.org> |
| In reply to | #79341 |
Öö Tiib <ootiib@hot.ee> wrote: > The focus has long gone away from considering each time we solve some > problem if the result is most efficient. On most cases it matters > if it works correctly for all input and if it is easy to understand what is > going on. Efficiency matters only for about 5% of code base. I don't see a problem in choosing the more efficient solution, especially if both solutions are approximately of the same complexity. The problem with thinking like "this is just like a millisecond slower, it doesn't really matter" is that when an entire huge program is full of such compromises, they stack up, and may add up to a significant slowdown. Multiply that millisecond by a thousand instances of doing the same thing, and suddenly your program takes a second to do something that it could be doing in a hundreth of a second. It will start feeling sluggish instead of responding immediately. It may be slow to react to things. If the program is being constantly run, doing that same thing over and over, suddenly it might take a minute to do something that it could be doing in one second. Then you wonder why programs seem to be getting slower and slower, even though hardware is getting faster and faster. It's exactly because of this kind of attitude by programmers. It's precisely because of the "efficiency only matters only for about 5% of code base". Because of the "it's just like a millisecond slower, it doesn't matter."
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2021-04-28 09:09 -0700 |
| Message-ID | <174354a1-ab2e-4fea-95bb-14dda18f37d6n@googlegroups.com> |
| In reply to | #79344 |
On Wednesday, 28 April 2021 at 08:21:47 UTC+3, Juha Nieminen wrote: > Öö Tiib <oot...@hot.ee> wrote: > > The focus has long gone away from considering each time we solve some > > problem if the result is most efficient. On most cases it matters > > if it works correctly for all input and if it is easy to understand what is > > going on. Efficiency matters only for about 5% of code base. > I don't see a problem in choosing the more efficient solution, especially > if both solutions are approximately of the same complexity. I don't see any problem either way as both seemed as error prone and used same magic number 5 ... I only thought wtf is that 5. > The problem with thinking like "this is just like a millisecond slower, > it doesn't really matter" is that when an entire huge program is full of > such compromises, they stack up, and may add up to a significant slowdown. > Multiply that millisecond by a thousand instances of doing the same thing, > and suddenly your program takes a second to do something that it could > be doing in a hundreth of a second. Nope. Paavo said that the difference was 4 nanoseconds. Multiplying 4 nanoseconds with thousand results with 4 microseconds. You need to multiply with 250 000 to first reach that millisecond. There are only rare entity types in programs whose counts ever reach such numbers and even with those you won only that millisecond. > It will start feeling sluggish instead > of responding immediately. It may be slow to react to things. If the > program is being constantly run, doing that same thing over and over, > suddenly it might take a minute to do something that it could be doing > in one second. Have you ever used profiler on some large product? It is not running all its code base over and over ever. Most code is ran next to never and so must be left exactly as inefficient as it was written. Changing it can just cause more defects in rarely verified places and so waste time into doing something meaningless and money into funding something counterproductive. Meanwhile lot (perhaps majority) of programs can be made orders of magnitude faster overall by concentrating the effort of choosing optimal algorithms to those few places where it matters because these places are ran over and over. > Then you wonder why programs seem to be getting slower and slower, > even though hardware is getting faster and faster. It's exactly because of > this kind of attitude by programmers. It's precisely because of the > "efficiency only matters only for about 5% of code base". Because of the > "it's just like a millisecond slower, it doesn't matter." Nonsense, I don't wonder, I have decades of evidence that the attitude is correct, and considering optimal when focus should be to robustness and correctness is worthless and counterproductive. The projects do not lag behind, get nowhere or fail exactly because of not tinkering over things that will never matter. And so there will be time to profile it and to optimize.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2021-04-29 04:45 +0000 |
| Message-ID | <s6ddkc$19ag$1@gioia.aioe.org> |
| In reply to | #79351 |
Öö Tiib <ootiib@hot.ee> wrote: > I don't see any problem either way as both seemed as error prone and used > same magic number 5 ... I only thought wtf is that 5. Are you always this nitpicky about examples (that have been simplified from the real-life counterparts that they are based on)? > Have you ever used profiler on some large product? If you have slight unnecessary inefficiencies at a thousand places in a huge program, the profiler isn't going to tell you where and which. The only thing it's going to tell you is that "this action is taking 1 second" but it won't be able to pinpoint the exact bottleneck... because the bottleneck will be about evenly distributed among a thousand instances of "this doesn't need to be efficient". It's, arguably, the worst possible bottleneck that could exist, because you can't pinpoint it. And fixing it is extraordinarily laborious, it will probably never be done, and the program will be doomed to being inefficient because nobody will fix it. All because of thousands of instances of the "this doesn't need to be efficient" attitude. > Nonsense, I don't wonder, I have decades of evidence that the attitude > is correct, and considering optimal when focus should be to robustness > and correctness is worthless and counterproductive. Yeah, because string::substr() is so much more robust and correct than string::compare(). I think that *deliberately* writing inefficient code is highly counterproductive.
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2021-04-29 15:16 -0700 |
| Message-ID | <3d6f3bcc-63a8-4498-9600-69a3c6b5088cn@googlegroups.com> |
| In reply to | #79360 |
On Thursday, 29 April 2021 at 07:45:17 UTC+3, Juha Nieminen wrote: > Öö Tiib <oot...@hot.ee> wrote: > > I don't see any problem either way as both seemed as error prone and used > > same magic number 5 ... I only thought wtf is that 5. > Are you always this nitpicky about examples (that have been simplified from > the real-life counterparts that they are based on)? Sorry, but those removed parts can actually show why it was done like that. > > Have you ever used profiler on some large product? > If you have slight unnecessary inefficiencies at a thousand places in a > huge program, the profiler isn't going to tell you where and which. > The only thing it's going to tell you is that "this action is taking > 1 second" but it won't be able to pinpoint the exact bottleneck... > because the bottleneck will be about evenly distributed among a > thousand instances of "this doesn't need to be efficient". No, you haven't used profiler? It always shows that only tiny subpart of whole program code is ran over 90% of time. Rest of code is ran more rarely and rarely. Half of most C++ code can be rewritten in Python and overall perceived performance of product does not change at all. > It's, arguably, the worst possible bottleneck that could exist, > because you can't pinpoint it. And fixing it is extraordinarily > laborious, it will probably never be done, and the program will be > doomed to being inefficient because nobody will fix it. All because > of thousands of instances of the "this doesn't need to be efficient" > attitude. Because that bottleneck does not exist. All that time wasted and 1% of performance gained. Meanwhile possible opportunities to improve performance several times lost. > > Nonsense, I don't wonder, I have decades of evidence that the attitude > > is correct, and considering optimal when focus should be to robustness > > and correctness is worthless and counterproductive. > Yeah, because string::substr() is so much more robust and correct than > string::compare(). For example the size() of all containers wasn't O(1) in C++98 but programmers used it to check if container was empty. It had to be made O(1) as it was somehow easier for programmers to reason. Same can be about your code that whatever you simplified out made that substr() easier to reason than compare(). > I think that *deliberately* writing inefficient code is highly > counterproductive. I don't think someone deliberately wrote c.size()==0 when it was less efficient than c.empty() ... or it++ when ++it was more efficient. Their focus was simply on things that mattered.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2021-04-30 10:10 +0000 |
| Message-ID | <s6gl2d$2v3$1@gioia.aioe.org> |
| In reply to | #79367 |
?? Tiib <ootiib@hot.ee> wrote: > Because that bottleneck does not exist. All that time wasted and 1% > of performance gained. Meanwhile possible opportunities to improve > performance several times lost. If an action that could take a hundreth of a second is instead taking a second, there certainly is a bottleneck. The problem is that if the bottleneck is distributed among thousands of sloppily written lines, each with an attitude of "this doesn't need to be efficient", you aren't going to fix that bottleneck any time soon. >> I think that *deliberately* writing inefficient code is highly >> counterproductive. > > I don't think someone deliberately wrote c.size()==0 when it was less > efficient than c.empty() ... or it++ when ++it was more efficient. Their > focus was simply on things that mattered. When the attitude is "this doesn't need to be efficient", and "it doesn't matter if this takes 10 times longer because the time is still in the microseconds", then you *are* deliberately and willingly writing inefficient code.
[toc] | [prev] | [next] | [standalone]
| From | Bo Persson <bo@bo-persson.se> |
|---|---|
| Date | 2021-04-30 15:33 +0200 |
| Message-ID | <if2fa3FohmjU1@mid.individual.net> |
| In reply to | #79369 |
On 2021-04-30 at 12:10, Juha Nieminen wrote: > ?? Tiib <ootiib@hot.ee> wrote: >> Because that bottleneck does not exist. All that time wasted and 1% >> of performance gained. Meanwhile possible opportunities to improve >> performance several times lost. > > If an action that could take a hundreth of a second is instead > taking a second, there certainly is a bottleneck. It's not a bottleneck if that program runs 3 times a day. At non peak hours. > > The problem is that if the bottleneck is distributed among > thousands of sloppily written lines, each with an attitude of > "this doesn't need to be efficient", you aren't going to fix > that bottleneck any time soon. > >>> I think that *deliberately* writing inefficient code is highly >>> counterproductive. >> >> I don't think someone deliberately wrote c.size()==0 when it was less >> efficient than c.empty() ... or it++ when ++it was more efficient. Their >> focus was simply on things that mattered. > > When the attitude is "this doesn't need to be efficient", and > "it doesn't matter if this takes 10 times longer because the > time is still in the microseconds", then you *are* deliberately > and willingly writing inefficient code. > It is often more efficient use of the developer's time to focus on the applications that has the most runtime on a specific system - perhaps using several CPU-hours per day. Not trying to get everything else down from 5 to 4 microseconds.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2021-05-04 10:32 +0000 |
| Message-ID | <s6r7se$1q3c$1@gioia.aioe.org> |
| In reply to | #79370 |
Bo Persson <bo@bo-persson.se> wrote: > It is often more efficient use of the developer's time to focus on the > applications that has the most runtime on a specific system - perhaps > using several CPU-hours per day. Not trying to get everything else down > from 5 to 4 microseconds. Or, conversely, it may be worth for the developer to spend an extra 10 hours of development time if the end result is a program that runs twice as fast, saving potentially thousands of CPU hours in the long run. Those microseconds add up when they are run many, many times.
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2021-05-01 06:51 -0700 |
| Message-ID | <566e0717-f1e7-4b47-8b1f-4a4d0d9685bfn@googlegroups.com> |
| In reply to | #79369 |
On Friday, 30 April 2021 at 13:10:39 UTC+3, Juha Nieminen wrote: > Öö Tiib <oot...@hot.ee> wrote: > > I don't think someone deliberately wrote c.size()==0 when it was less > > efficient than c.empty() ... or it++ when ++it was more efficient. Their > > focus was simply on things that mattered. > > When the attitude is "this doesn't need to be efficient", and > "it doesn't matter if this takes 10 times longer because the > time is still in the microseconds", then you *are* deliberately > and willingly writing inefficient code. But performance does not *actually* matter most of software development life cycle. Donald Knuth's fat volumes of science of optimizing algorithms are outright admirable. Yet he wrote in "Computer Programming as an Art" (1974) golden words that every programmer I know can cite "premature optimization is the root of all evil". So come back from dark and evil side, Juha. Performance starts to matter only after correctness has been reached from end to end and then only to minor subpart of whole program.
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2021-05-03 21:38 +0300 |
| Message-ID | <s6pfve$80d$1@dont-email.me> |
| In reply to | #79386 |
01.05.2021 16:51 Öö Tiib kirjutas: > On Friday, 30 April 2021 at 13:10:39 UTC+3, Juha Nieminen wrote: >> Öö Tiib <oot...@hot.ee> wrote: >>> I don't think someone deliberately wrote c.size()==0 when it was less >>> efficient than c.empty() ... or it++ when ++it was more efficient. Their >>> focus was simply on things that mattered. >> >> When the attitude is "this doesn't need to be efficient", and >> "it doesn't matter if this takes 10 times longer because the >> time is still in the microseconds", then you *are* deliberately >> and willingly writing inefficient code. > > But performance does not *actually* matter most of software > development life cycle. Donald Knuth's fat volumes of science of > optimizing algorithms are outright admirable. Yet he wrote in > "Computer Programming as an Art" (1974) golden words that > every programmer I know can cite "premature optimization is > the root of all evil". So come back from dark and evil side, Juha. > Performance starts to matter only after correctness has been > reached from end to end and then only to minor subpart of > whole program. That's true, but sometimes the subpart where performance matters is not known beforehand. A lot of C++ code goes into reusable libraries, and when writing a library function it is often not known how many times it will get called by different client programs. So if there is an easy way to write some code line to run 10x faster, in library code it should be done IMO.
[toc] | [prev] | [next] | [standalone]
| From | Öö Tiib <ootiib@hot.ee> |
|---|---|
| Date | 2021-05-06 18:21 -0700 |
| Message-ID | <7d0d32dc-2c72-4bb1-aabd-dc878d0e00bdn@googlegroups.com> |
| In reply to | #79405 |
On Monday, 3 May 2021 at 21:38:53 UTC+3, Paavo Helde wrote: > 01.05.2021 16:51 Öö Tiib kirjutas: > > On Friday, 30 April 2021 at 13:10:39 UTC+3, Juha Nieminen wrote: > >> Öö Tiib <oot...@hot.ee> wrote: > >>> I don't think someone deliberately wrote c.size()==0 when it was less > >>> efficient than c.empty() ... or it++ when ++it was more efficient. Their > >>> focus was simply on things that mattered. > >> > >> When the attitude is "this doesn't need to be efficient", and > >> "it doesn't matter if this takes 10 times longer because the > >> time is still in the microseconds", then you *are* deliberately > >> and willingly writing inefficient code. > > > > But performance does not *actually* matter most of software > > development life cycle. Donald Knuth's fat volumes of science of > > optimizing algorithms are outright admirable. Yet he wrote in > > "Computer Programming as an Art" (1974) golden words that > > every programmer I know can cite "premature optimization is > > the root of all evil". So come back from dark and evil side, Juha. > > Performance starts to matter only after correctness has been > > reached from end to end and then only to minor subpart of > > whole program. > That's true, but sometimes the subpart where performance matters is not > known beforehand. A lot of C++ code goes into reusable libraries, and > when writing a library function it is often not known how many times it > will get called by different client programs. Yes. I agree. You are saying same thing. The subpart where performance matters often becomes known only after correctness has been reached from end to end and the code is put into use by different clients. Usually because it does everything it was intended to do and nothing that it wasn't intended to do. Often the context and data we considered "realistic" when we profiled it was far from needs that arise in few years. But fortunately software can be profiled and improved in few years too. > So if there is an easy way > to write some code line to run 10x faster, in library code it should be > done IMO. But you somehow conclude from above that that we should ponder each line of code if there is some easier or harder way to make it faster. Why? The whole file with that line may be will be erased entirely when we get actual performance issue and data with what to profile.
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2021-05-07 12:05 +0300 |
| Message-ID | <s72vt9$j4a$1@dont-email.me> |
| In reply to | #79433 |
07.05.2021 04:21 Öö Tiib kirjutas: > On Monday, 3 May 2021 at 21:38:53 UTC+3, Paavo Helde wrote: > >> So if there is an easy way >> to write some code line to run 10x faster, in library code it should be >> done IMO. > > But you somehow conclude from above that that we should ponder each > line of code if there is some easier or harder way to make it faster. Why? > The whole file with that line may be will be erased entirely when we get > actual performance issue and data with what to profile. For me it seems very easy, the programmer anyway needs to choose for each line how they write it. For each line or task there are many ways to code it incorrectly, there are some ways to code it more or less correctly, and then there are optimizations which might not be obvious, so cannot be taken into account when first writing that line. This correctness for me also includes reasonable performance. For example, choosing a solution with O(log N) complexity over O(N). Even though the latter would also work and produce the needed result, it would not be a "correct" solution in my book, unless it is known and documented that the function will only be used with very small N. The time and effort for documenting and checking this assumption may easily turn out to be more time consuming than using the correct O(log N) algorithm in the first place. For the OP's example, there are two ways to compare the substring in C++, either with string::substr()== or with string::compare(). In my book, as the former is 2-10x slower than the latter, only the latter is a "correct" way to code this line IMO. When the compilers get smarter and become able to optimize them to the same speed, one can reconsider. That's the same as with passing by value or by reference. In C++ one should prefer passing non-trivial objects by reference if possible, as pass by value might often waste both speed and memory for no reason. Yet, passing by reference involves writing some more keystrokes, so according to some opinions which I have seen in this thread it should not be done before profiler shows there is a bottle-neck.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2021-05-04 10:43 +0000 |
| Message-ID | <s6r8h6$9ar$1@gioia.aioe.org> |
| In reply to | #79386 |
Öö Tiib <ootiib@hot.ee> wrote: > But performance does not *actually* matter most of software > development life cycle. Donald Knuth's fat volumes of science of > optimizing algorithms are outright admirable. Yet he wrote in > "Computer Programming as an Art" (1974) golden words that > every programmer I know can cite "premature optimization is > the root of all evil". So come back from dark and evil side, Juha. > Performance starts to matter only after correctness has been > reached from end to end and then only to minor subpart of > whole program. When Knuth wrote "premature optimization is the root of all evil" in 1974, I doubt he was thinking about std::string::substr() vs. std::string::compare() in a language standardized in 1998. In fact, I personally consider the (in)famous adage to have caused more harm than good overall because way, way, way too many programmers have interpreted it to mean "deliberately and purposefully make your program as inefficient as you possibly can, because optimization, any kind of optimization, is one of the worst things you could possibly do". I'm not even kidding. I have encountered that sort of attitude in the real world out there many times. People deliberately choosing a less efficient algorithm or data container even when a significantly more efficient one is available, because "premature optimization is the root of all evil", and there's literally zero reason not to use the more efficient version. I don't think many people even understand what he meant by it. Does anybody understand what he meant by it? By "premature optimization" didn't he merely mean ugly hacks that supposedly make the program a few clock cycles faster at the cost of making it almost unreadable (something that was especially common in the 70's and 80's, when compilers were extraordinarily basic and bad at optimization). Such as explicitly using bitshifting instead of multiplication, manually inlining code, and other such hacks. The more such hacks are used, the less readable and maintainable the code belongs, and it becomes. "Premature optimization" does not mean using quicksort instead of bubble sort. Or using a balanced binary tree instead of an array (and linear searches). Or using std::string::compare() instead of std::string::substr().
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2021-05-04 15:20 +0200 |
| Message-ID | <s6rhma$f9h$1@dont-email.me> |
| In reply to | #79410 |
On 04/05/2021 12:43, Juha Nieminen wrote: > Öö Tiib <ootiib@hot.ee> wrote: >> But performance does not *actually* matter most of software >> development life cycle. Donald Knuth's fat volumes of science of >> optimizing algorithms are outright admirable. Yet he wrote in >> "Computer Programming as an Art" (1974) golden words that >> every programmer I know can cite "premature optimization is >> the root of all evil". So come back from dark and evil side, Juha. >> Performance starts to matter only after correctness has been >> reached from end to end and then only to minor subpart of >> whole program. > > When Knuth wrote "premature optimization is the root of all evil" > in 1974, I doubt he was thinking about std::string::substr() > vs. std::string::compare() in a language standardized in 1998. > > In fact, I personally consider the (in)famous adage to have caused > more harm than good overall because way, way, way too many programmers > have interpreted it to mean "deliberately and purposefully make your > program as inefficient as you possibly can, because optimization, > any kind of optimization, is one of the worst things you could > possibly do". > > I'm not even kidding. I have encountered that sort of attitude in the > real world out there many times. People deliberately choosing a less > efficient algorithm or data container even when a significantly > more efficient one is available, because "premature optimization > is the root of all evil", and there's literally zero reason not to > use the more efficient version. > I have certainly seen code written without any thought given to efficiency (like "x * 0.5" instead of "x / 2" on an 8-bit embedded system). But I don't believe I have seen anyone deliberately choosing a less efficient algorithm when the efficiency was the only difference. > I don't think many people even understand what he meant by it. > Does anybody understand what he meant by it? By "premature optimization" > didn't he merely mean ugly hacks that supposedly make the program > a few clock cycles faster at the cost of making it almost unreadable > (something that was especially common in the 70's and 80's, when > compilers were extraordinarily basic and bad at optimization). > Such as explicitly using bitshifting instead of multiplication, > manually inlining code, and other such hacks. The more such hacks > are used, the less readable and maintainable the code belongs, and it > becomes. > > "Premature optimization" does not mean using quicksort instead of > bubble sort. Or using a balanced binary tree instead of an array (and > linear searches). Or using std::string::compare() instead of > std::string::substr(). > For most code, efficiency does not particularly matter (pick your own favourite statistic - 95.32% of run time is spent inside 2.12% of the code). It might begin to matter if the bulk of the code uses really bad algorithms, but you don't need to put special effort into it. In particular, you should not optimise for efficiency at the expense of making code less clear, or increasing the risk of making it incorrect. I can't remember if it was Knuth or someone else who said that optimising for speed is easy if you don't worry about correctness. There are good reasons why most code these days is not written in compiled languages - developer time, ease of maintenance, flexibility, etc., are often more important than absolute speed. But premature pessimisation follows close behind premature optimisation as the root of all evil. So it might be better to write a piece of code in Python rather than C++, but you still want to make sure you use lists or dicts as appropriate and avoid needlessly inefficient algorithms.
[toc] | [prev] | [next] | [standalone]
| From | Manfred <noname@add.invalid> |
|---|---|
| Date | 2021-05-04 16:41 +0200 |
| Message-ID | <s6rmet$1kfn$1@gioia.aioe.org> |
| In reply to | #79412 |
On 5/4/2021 3:20 PM, David Brown wrote: > On 04/05/2021 12:43, Juha Nieminen wrote: >> Öö Tiib <ootiib@hot.ee> wrote: >>> But performance does not *actually* matter most of software >>> development life cycle. Donald Knuth's fat volumes of science of >>> optimizing algorithms are outright admirable. Yet he wrote in >>> "Computer Programming as an Art" (1974) golden words that >>> every programmer I know can cite "premature optimization is >>> the root of all evil". So come back from dark and evil side, Juha. >>> Performance starts to matter only after correctness has been >>> reached from end to end and then only to minor subpart of >>> whole program. >> >> When Knuth wrote "premature optimization is the root of all evil" >> in 1974, I doubt he was thinking about std::string::substr() >> vs. std::string::compare() in a language standardized in 1998. >> >> In fact, I personally consider the (in)famous adage to have caused >> more harm than good overall because way, way, way too many programmers >> have interpreted it to mean "deliberately and purposefully make your >> program as inefficient as you possibly can, because optimization, >> any kind of optimization, is one of the worst things you could >> possibly do". >> >> I'm not even kidding. I have encountered that sort of attitude in the >> real world out there many times. People deliberately choosing a less >> efficient algorithm or data container even when a significantly >> more efficient one is available, because "premature optimization >> is the root of all evil", and there's literally zero reason not to >> use the more efficient version. >> > > I have certainly seen code written without any thought given to > efficiency (like "x * 0.5" instead of "x / 2" on an 8-bit embedded > system). But I don't believe I have seen anyone deliberately choosing a > less efficient algorithm when the efficiency was the only difference. > >> I don't think many people even understand what he meant by it. >> Does anybody understand what he meant by it? By "premature optimization" >> didn't he merely mean ugly hacks that supposedly make the program >> a few clock cycles faster at the cost of making it almost unreadable >> (something that was especially common in the 70's and 80's, when >> compilers were extraordinarily basic and bad at optimization). >> Such as explicitly using bitshifting instead of multiplication, >> manually inlining code, and other such hacks. The more such hacks >> are used, the less readable and maintainable the code belongs, and it >> becomes. >> >> "Premature optimization" does not mean using quicksort instead of >> bubble sort. Or using a balanced binary tree instead of an array (and >> linear searches). Or using std::string::compare() instead of >> std::string::substr(). >> > > For most code, efficiency does not particularly matter (pick your own > favourite statistic - 95.32% of run time is spent inside 2.12% of the > code). It might begin to matter if the bulk of the code uses really bad > algorithms, but you don't need to put special effort into it. In > particular, you should not optimise for efficiency at the expense of > making code less clear, or increasing the risk of making it incorrect. > I can't remember if it was Knuth or someone else who said that > optimising for speed is easy if you don't worry about correctness. > > There are good reasons why most code these days is not written in > compiled languages - developer time, ease of maintenance, flexibility, > etc., are often more important than absolute speed. > > But premature pessimisation follows close behind premature optimisation > as the root of all evil. So it might be better to write a piece of code > in Python rather than C++, but you still want to make sure you use lists > or dicts as appropriate and avoid needlessly inefficient algorithms. > You are bringing on an important aspect here. I have experienced too the fact that performance really matters in a small fraction of code, however I am seeing the opposite attitude growing up to a point that the performance hit becomes actually relevant even for ordinary applications. The most relevant issue is not really about writing one piece of C or C++ code in one way or the other - what is becoming (has become) relevant IMO is pervasive use of managed languages because of their appeal to make programming "easy" at the cost of some performance hit that is supposed to be not that relevant after all with nowadays' hardware. This marketing narrative may be appealing to some management that has some financially biased perspective on sw development - be as it may, the fact is that it is quite common to run into some trivial application that for the sole purpose of keeping running needs to load dozens of DLLs. The issue even stops being a matter of preventing "premature optimization", i.e. favor simplicity over sophistication for the purpose of ensuring correctness. When you build something that needs a ton of subsystems for the the sake of its own language infrastructure you are still introducing extra and brittle sophistication even if it is hidden by the curtain of apparently compact source code.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Date | 2021-05-04 23:42 -0400 |
| Message-ID | <s6t47a$6r5$1@dont-email.me> |
| In reply to | #79410 |
On 5/4/21 6:43 AM, Juha Nieminen wrote: ...> When Knuth wrote "premature optimization is the root of all evil" > in 1974, I doubt he was thinking about std::string::substr() > vs. std::string::compare() in a language standardized in 1998. The fact that the specifics of a particular optimization were unknown to him doesn't render his advice about optimization irrelevant to that decision. It was very general advice, not specific to any particular programming language or development environment. > In fact, I personally consider the (in)famous adage to have caused > more harm than good overall because way, way, way too many programmers > have interpreted it to mean "deliberately and purposefully make your > program as inefficient as you possibly can, because optimization, > any kind of optimization, is one of the worst things you could > possibly do". > > I'm not even kidding. I have encountered that sort of attitude in the > real world out there many times. People deliberately choosing a less > efficient algorithm or data container even when a significantly > more efficient one is available, because "premature optimization > is the root of all evil", and there's literally zero reason not to > use the more efficient version. If you say you've seen such behavior, I can't prove you wrong, but I would consider any developer who did such a thing insane. Such behavior is pretty much completely unrelated to what Knuth actually meant. > I don't think many people even understand what he meant by it. > Does anybody understand what he meant by it? By "premature optimization" > didn't he merely mean ugly hacks that supposedly make the program > a few clock cycles faster at the cost of making it almost unreadable > (something that was especially common in the 70's and 80's, when > compilers were extraordinarily basic and bad at optimization). > Such as explicitly using bitshifting instead of multiplication, > manually inlining code, and other such hacks. The more such hacks > are used, the less readable and maintainable the code belongs, and it > becomes. While those are the worst examples of premature optimization, any time you have a choice between a clear, easily understood algorithm, and a more efficient algorithm that's less easy to understand, Knuth's adage quite rightly favors using the clearer one rather than the more efficient one, unless and until you've determined through such methods as profiling that it's sufficiently less efficient for the difference to matter. I could easily imagine someone who was not sympathetic to such advice incorrectly describing such a decision the way you did above. The key thing that would be incorrect about doing so would the claim that "there's literally zero reason not to use the more efficient version". Greater clarity can be a very legitimate reason for such a decision.
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2021-05-05 05:01 +0000 |
| Message-ID | <s6t8r6$1j34$1@gioia.aioe.org> |
| In reply to | #79417 |
James Kuyper <jameskuyper@alumni.caltech.edu> wrote: > While those are the worst examples of premature optimization, any time > you have a choice between a clear, easily understood algorithm, and a > more efficient algorithm that's less easy to understand, Knuth's adage > quite rightly favors using the clearer one rather than the more > efficient one, unless and until you've determined through such methods > as profiling that it's sufficiently less efficient for the difference to > matter. I could easily imagine someone who was not sympathetic to such > advice incorrectly describing such a decision the way you did above. The > key thing that would be incorrect about doing so would the claim that > "there's literally zero reason not to use the more efficient version". > Greater clarity can be a very legitimate reason for such a decision. The argument has been made that getting the program working and stable in a timely manner is more important than whether it happens to be a few seconds faster or not. After all, in this busy world we don't have time to waste on such trivialities as code optimization. We need a working product yesterday! We don't have time nor money to make it actually efficient! The thing is, if that's the approach, then do you really think that such developers will later come back to the product and go through the time and effort to profiling it and refactoring it to make it more efficient? After all, writing the inefficient version and then the more efficient version is more time spent than merely writing the inefficient version and doing nothing more. So, the best way to avoid this is to write the more efficient version from the get-go! Then there's no need to go back and profile the program and refactor it (which sometimes may mean extensive changes to the program). Sure, maybe if the "KISS" version of the routine takes 1 minute to write and the more efficient version takes 3 hours to write, there might be an argument there. However, the more efficient version takes 3 minutes to write, I would argue it's worth it.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2021-05-05 11:26 +0200 |
| Message-ID | <s6tobh$jp8$1@dont-email.me> |
| In reply to | #79418 |
On 05/05/2021 07:01, Juha Nieminen wrote: > James Kuyper <jameskuyper@alumni.caltech.edu> wrote: >> While those are the worst examples of premature optimization, any time >> you have a choice between a clear, easily understood algorithm, and a >> more efficient algorithm that's less easy to understand, Knuth's adage >> quite rightly favors using the clearer one rather than the more >> efficient one, unless and until you've determined through such methods >> as profiling that it's sufficiently less efficient for the difference to >> matter. I could easily imagine someone who was not sympathetic to such >> advice incorrectly describing such a decision the way you did above. The >> key thing that would be incorrect about doing so would the claim that >> "there's literally zero reason not to use the more efficient version". >> Greater clarity can be a very legitimate reason for such a decision. > > The argument has been made that getting the program working and stable in > a timely manner is more important than whether it happens to be a few > seconds faster or not. After all, in this busy world we don't have time > to waste on such trivialities as code optimization. We need a working > product yesterday! We don't have time nor money to make it actually > efficient! That is certainly correct for some systems. > > The thing is, if that's the approach, then do you really think that > such developers will later come back to the product and go through the > time and effort to profiling it and refactoring it to make it more > efficient? For a great deal of code written, there is no "later". Many products - and the software in them - have short lifetimes. If you spend a lot of time developing efficient solutions, the product will never exist - while you have been ensuring your system reacts in 10 milliseconds instead of 100 milliseconds, competing companies have taken the market and you (or your client) is out of business. The competing company won't bother optimising their software efficiency because they will only be making the product for 6 months before moving on to something new. Software development means getting the balance right between the different costs in time, money, resources, etc. If time to market is the key issue, then optimising the software development might mean using off-the-shelf embedded Linux boards running code in Python even though you are just controlling a few buttons and a small motor that could be controlled by a $1 microcontroller. If developer costs are the key issue, you use whatever language that one developer knows best. If production costs are the key issue, you use the cheapest microcontroller. If battery lifetimes are key, you optimise there. There are only two things you can be sure about. One is that if you concentrate /solely/ on run-time speed efficiency of the code, you will be wrong more often than you are right. The other is that no matter what the time or money pressure, getting the software /right/ trumps all other considerations. Knuth's observation is not about optimisation of code speed - it is about optimisation of the development process. > > After all, writing the inefficient version and then the more efficient > version is more time spent than merely writing the inefficient version > and doing nothing more. > And if writing the inefficient version takes less time than writing the efficient version, then often it is the right answer. Computer time is vastly cheaper than developer time - you don't spend even an hour of developer time to save a millisecond of run time unless that code is going to be run at least a million million times. /Some/ code is run that often, but most code is not. > So, the best way to avoid this is to write the more efficient version > from the get-go! Then there's no need to go back and profile the program > and refactor it (which sometimes may mean extensive changes to the > program). > > Sure, maybe if the "KISS" version of the routine takes 1 minute to write > and the more efficient version takes 3 hours to write, there might be an > argument there. However, the more efficient version takes 3 minutes to > write, I would argue it's worth it. >
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c++
csiph-web