Sujet : Re: strlcpy and how CPUs can defy common sense
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.miscDate : 26. Jul 2024, 17:19:01
Autres entêtes
Organisation : Stefan Ram
Message-ID : <strings-20240726170151@ram.dialup.fu-berlin.de>
References : 1
Ben Collver <
bencollver@tilde.pink> wrote or quoted:
Hidden in this argument is the assumption that traversing the string
once is faster. Which - to be clear - is not at all an unreasonable
assumption. But is it actually true? That's the focus of today's
article.
If the string is long, it might not fit into some level caches,
meaning it would need to be fetched from main memory twice,
which is a drag. But if the string is short, that's not the
case, and looping through it twice doesn't have to be slower.
surprises. And so the performance of an algorithm doesn't just depend
on high level algorithmic factors - lower level factors such as cache
misses, ILP, branch mispredictions etc, also need to taken into
account. Many things which seems to be faster from a common sense
perspective might in practice end up being slower and vice versa.
Especially when it comes to cache misses. And for loops,
there's also the regularity factor.
This video might be a hit with some folks: "The strange details
of std__string at Facebook" - Nicholas Ormrod, CppCon 2016.
And who said this?
|Rob Pike's 5 Rules of Programming
|
|Rule 1. You can't tell where a program is going to spend
|its time. Bottlenecks occur in surprising places, so don't
|try to second guess and put in a speed hack until you've
|proven that's where the bottleneck is.
|
|Rule 2. Measure. Don't tune for speed until you've measured, and
|even then don't unless one part of the code overwhelms the rest.
|
|Rule 3. Fancy algorithms are slow when n is small, and n is
|usually small. Fancy algorithms have big constants. Until you
|know that n is frequently going to be big, don't get fancy.
|(Even if n does get big, use Rule 2 first.)
|
|Rule 4. Fancy algorithms are buggier than simple ones, and
|they're much harder to implement. Use simple algorithms as
|well as simple data structures.
|
|Rule 5. Data dominates. If you've chosen the right data
|structures and organized things well, the algorithms will
|almost always be self-evident. Data structures, not algorithms,
|are central to programming.
If you said, "Rob Pike", you were right!