Why is sorting a pointer-array to strings slower than sorting the[...]
strings themselfes ? I've checked that with that code:
I've read 1.4 million strings with an average lengh of 9 characters,
so most strings should fall under the short string optimization and
swapping the string-objects would have a higher cost than swapping
two pointers or a pointer and a length.
And sorting string-pointers
is actually nearly twice times slower than sorting the string-objects.
Why is that ?
Am 29.11.21 um 21:26 schrieb Bonita Montero:
Why is sorting a pointer-array to strings slower than sorting the[...]
strings themselfes ? I've checked that with that code:
I've read 1.4 million strings with an average lengh of 9 characters,
so most strings should fall under the short string optimization and
swapping the string-objects would have a higher cost than swapping
two pointers or a pointer and a length.
Swapping a few bytes is not the performance killer. Cache misses are the >performance killer. Every time you touch a memory address not in the
cache you get the latency of the RAM. And this latency has not improved >significantly in the last two decades.
Am 30.11.2021 um 15:58 schrieb Scott Lurndal:
Define significantly. Mid 2000's server DRAM access latency could
be as much as 250ns (Intel Westmere), while modern DRAM latency is
closer to 50-60ns. That's a five-fold improvement (granted westmere
was abnormally slow when accessing the second socket).
Westmere was 2010:
https://en.wikipedia.org/wiki/Westmere_(microarchitecture)
Define significantly. Mid 2000's server DRAM access latency could
be as much as 250ns (Intel Westmere), while modern DRAM latency is
closer to 50-60ns. That's a five-fold improvement (granted westmere
was abnormally slow when accessing the second socket).
I never, ever, mentioned 128-byte cache lines. Why do you insist
on changing the topic, which was memory latency; which on Nahalem
was significantly higher than today's CPUs (mainly due to inter-socket communication latency, being a NUMA architecture).
Bonita Montero <Bonita.Montero@gmail.com> writes:
Am 30.11.2021 um 15:58 schrieb Scott Lurndal:
Define significantly. Mid 2000's server DRAM access latency could
be as much as 250ns (Intel Westmere), while modern DRAM latency is
closer to 50-60ns. That's a five-fold improvement (granted westmere
was abnormally slow when accessing the second socket).
Westmere was 2010:
https://en.wikipedia.org/wiki/Westmere_(microarchitecture)
Nahelem suffered from the same issue, and we had samples of
westmere in 2008.
Am 30.11.2021 um 17:02 schrieb Scott Lurndal:
Bonita Montero <Bonita.Montero@gmail.com> writes:
Am 30.11.2021 um 15:58 schrieb Scott Lurndal:
Define significantly. Mid 2000's server DRAM access latency could
be as much as 250ns (Intel Westmere), while modern DRAM latency is
closer to 50-60ns. That's a five-fold improvement (granted westmere
was abnormally slow when accessing the second socket).
Westmere was 2010:
https://en.wikipedia.org/wiki/Westmere_(microarchitecture)
Nahelem suffered from the same issue, and we had samples of
westmere in 2008.
No, there was no CPU-architecture except the Pentium 4 that had
128 byte cachelines.
Swapping a few bytes is not the performance killer. Cache misses are the
performance killer. Every time you touch a memory address not in the
cache you get the latency of the RAM. And this latency has not improved
significantly in the last two decades.
Define significantly. Mid 2000's server DRAM access latency could
be as much as 250ns (Intel Westmere), while modern DRAM latency is
closer to 50-60ns.
Why is sorting a pointer-array to strings slower than sorting the
strings themselfes ?i
On Monday, 29 November 2021 at 22:26:35 UTC+2, Bonita Montero wrote:
Why is sorting a pointer-array to strings slower than sorting the
strings themselfes ?i
Because std::string itself is kind of "smart pointer" of text it contains. Its swap after compare costs next to nothing. So using additional
indirection and memory for raw pointers there is clear "preliminary pessimization" ... more complicated code for no gain.
Am 30.11.2021 um 22:26 schrieb Öö Tiib:
On Monday, 29 November 2021 at 22:26:35 UTC+2, Bonita Montero wrote:
Why is sorting a pointer-array to strings slower than sorting the
strings themselfes ?i
Because std::string itself is kind of "smart pointer" of text it contains. Its swap after compare costs next to nothing. So using additional indirection and memory for raw pointers there is clear "preliminary pessimization" ... more complicated code for no gain.I already said that I'm swapping strings that fit inside the string
-objects themselfes. They have a average length of 9 chars and they
fit inside the small string optimization.
On Wednesday, 1 December 2021 at 09:29:19 UTC+2, Bonita Montero wrote:
Am 30.11.2021 um 22:26 schrieb Öö Tiib:
On Monday, 29 November 2021 at 22:26:35 UTC+2, Bonita Montero wrote:I already said that I'm swapping strings that fit inside the string
Why is sorting a pointer-array to strings slower than sorting the
strings themselfes ?i
Because std::string itself is kind of "smart pointer" of text it contains. >>> Its swap after compare costs next to nothing. So using additional
indirection and memory for raw pointers there is clear "preliminary
pessimization" ... more complicated code for no gain.
-objects themselfes. They have a average length of 9 chars and they
fit inside the small string optimization.
What difference does it supposedly make? ...
Am 01.12.2021 um 22:36 schrieb Öö Tiib:
On Wednesday, 1 December 2021 at 09:29:19 UTC+2, Bonita Montero wrote:
Am 30.11.2021 um 22:26 schrieb Öö Tiib:
On Monday, 29 November 2021 at 22:26:35 UTC+2, Bonita Montero wrote:I already said that I'm swapping strings that fit inside the string
Why is sorting a pointer-array to strings slower than sorting the
strings themselfes ?i
Because std::string itself is kind of "smart pointer" of text it contains. >>>> Its swap after compare costs next to nothing. So using additional
indirection and memory for raw pointers there is clear "preliminary
pessimization" ... more complicated code for no gain.
-objects themselfes. They have a average length of 9 chars and they
fit inside the small string optimization.
What difference does it supposedly make? ...
It makes the difference that you swap the string-contents byte-by-byte
and not with two pointer-swaps. Loading or storing a byte costs the same
like loading or storing a pointer.
Bonita Montero <Bonita.Montero@gmail.com> wrote:
Am 01.12.2021 um 22:36 schrieb Öö Tiib:
On Wednesday, 1 December 2021 at 09:29:19 UTC+2, Bonita Montero wrote:
Am 30.11.2021 um 22:26 schrieb Öö Tiib:
On Monday, 29 November 2021 at 22:26:35 UTC+2, Bonita Montero wrote: >>>>>> Why is sorting a pointer-array to strings slower than sorting theI already said that I'm swapping strings that fit inside the string
strings themselfes ?i
Because std::string itself is kind of "smart pointer" of text it contains.
Its swap after compare costs next to nothing. So using additional
indirection and memory for raw pointers there is clear "preliminary
pessimization" ... more complicated code for no gain.
-objects themselfes. They have a average length of 9 chars and they
fit inside the small string optimization.
What difference does it supposedly make? ...
It makes the difference that you swap the string-contents byte-by-byte
and not with two pointer-swaps. Loading or storing a byte costs the same
like loading or storing a pointer.
If the string objects are indeed using short string optimization, I doubt that the object contents are being swapped byte-by-byte. Most likely
8 bytes at a time (just like the pointers).
One advantage over using pointers to the objects is, of course, that you don't get the extra indirection.
With MSVC, clang and g++ the contents are swapped as four SSE
128 bit words; the size of the string or if its external or
internal is ignored.
But it's more effort to swap four 128 bit
words than two pointers.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 113 |
Nodes: | 8 (1 / 7) |
Uptime: | 134:43:15 |
Calls: | 2,502 |
Calls today: | 1 |
Files: | 8,696 |
Messages: | 1,925,790 |