Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Machines that have broken down will work perfectly when the repairman arrives.


devel / comp.lang.c / Re: qsort() vs. std::sort

SubjectAuthor
* qsort() vs. std::sortBonita Montero
+- Re: qsort() vs. std::sortBonita Montero
+* Re: qsort() vs. std::sortfir
|+- Re: qsort() vs. std::sortfir
|`* Re: qsort() vs. std::sortBonita Montero
| +* Re: qsort() vs. std::sortfir
| |`* Re: qsort() vs. std::sortBonita Montero
| | `* Re: qsort() vs. std::sortfir
| |  `* Re: qsort() vs. std::sortBonita Montero
| |   `- Re: qsort() vs. std::sortfir
| `* Re: qsort() vs. std::sortDolores Filandro
|  +* Re: qsort() vs. std::sortAndrey Tarasevich
|  |`* The missing sgn() function (Was: qsort() vs. std::sort)Kenny McCormack
|  | `* Re: The missing sgn() function (Was: qsort() vs. std::sort)Andrey Tarasevich
|  |  `* Re: The missing sgn() function (Was: qsort() vs. std::sort)Andrey Tarasevich
|  |   `* Re: The missing sgn() function (Was: qsort() vs. std::sort)Dolores Filandro
|  |    `* Re: The missing sgn() function (Was: qsort() vs. std::sort)Malcolm McLean
|  |     `- Re: The missing sgn() functionKeith Thompson
|  +* Re: qsort() vs. std::sortStefan Ram
|  |`- Re: qsort() vs. std::sortStefan Ram
|  `* Re: qsort() vs. std::sortBonita Montero
|   +* Re: qsort() vs. std::sortScott Lurndal
|   |`- Re: qsort() vs. std::sortBonita Montero
|   `* Re: qsort() vs. std::sortAndrey Tarasevich
|    `* Re: qsort() vs. std::sortBonita Montero
|     +* Re: qsort() vs. std::sortÖö Tiib
|     |`* Re: qsort() vs. std::sortBonita Montero
|     | `* Re: qsort() vs. std::sortjames...@alumni.caltech.edu
|     |  `* Re: qsort() vs. std::sortBonita Montero
|     |   `* Re: qsort() vs. std::sortjames...@alumni.caltech.edu
|     |    `- Re: qsort() vs. std::sortBonita Montero
|     `- Re: qsort() vs. std::sortjames...@alumni.caltech.edu
+* Re: qsort() vs. std::sortMalcolm McLean
|+- Re: qsort() vs. std::sortfir
|`- Re: qsort() vs. std::sortBonita Montero
+* Re: qsort() vs. std::sortJuha Nieminen
|+- Re: qsort() vs. std::sortBonita Montero
|`- Re: qsort() vs. std::sortMalcolm McLean
+* Re: qsort() vs. std::sortThiago Adams
|`* Re: qsort() vs. std::sortBonita Montero
| `* Re: qsort() vs. std::sortWilliam Ahern
|  `* Re: qsort() vs. std::sortBonita Montero
|   +* Re: qsort() vs. std::sortWilliam Ahern
|   |`- Re: qsort() vs. std::sortMalcolm McLean
|   `- Re: qsort() vs. std::sortThiago Adams
`* Re: qsort() vs. std::sortWilliam Ahern
 +- Re: qsort() vs. std::sortBonita Montero
 `* Re: qsort() vs. std::sortJuha Nieminen
  `- Re: qsort() vs. std::sortWilliam Ahern

Pages:12
qsort() vs. std::sort

<t4092b$des$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21394&group=comp.lang.c#21394

  copy link   Newsgroups: comp.lang.c++ comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c
Subject: qsort() vs. std::sort
Date: Sat, 23 Apr 2022 09:15:42 +0200
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <t4092b$des$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 23 Apr 2022 07:15:23 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f8878cd36041ec7a8da220aa265ccafb";
logging-data="13788"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18OqbeTM060zQQNggtNGfAQsKtva3WWSBQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:xn3kOWQj316g5zT9BH/nFn50H5s=
Content-Language: de-DE
 by: Bonita Montero - Sat, 23 Apr 2022 07:15 UTC

I just measured how superior C++'s std::sort is over C's qsort:

#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <cstdlib>
#include <algorithm>
#include <memory>
#include <atomic>

using namespace std;
using namespace chrono;

atomic_uint auNonOptimize;

int main()
{ constexpr size_t
V_MAX = 0x1000,
V_MAX_ROUNDS = 0x4000;
vector<unsigned>
vu( V_MAX, 0 ),
vuSort( V_MAX, 0 );
mt19937_64 mt;
uniform_int_distribution<unsigned> uid( 0, -1 );
for( unsigned &u : vu )
u = uid( mt );
auto timedSort = [&]<typename SortFn>( SortFn sortFn, size_t n, size_t
rounds ) -> double
requires requires( SortFn sortFn, unsigned *p ) { { sortFn( p, p ) }; }
{
auto start = high_resolution_clock::now();
unsigned sum = 0;
while( rounds-- )
copy( vu.begin(), vu.begin() + n, vuSort.begin() ),
sortFn( to_address( vuSort.begin() ), to_address( vuSort.begin() ) + n ),
sum += vu[0];
::auNonOptimize = sum;
return (int64_t)duration_cast<nanoseconds>(
high_resolution_clock::now() - start ).count() / 1.0e9;
};
auto cSort = []( unsigned *begin, unsigned *end )
{
qsort( begin, end - begin, sizeof(unsigned),
[]( void const *left, void const *right ) -> int
{
unsigned
&l = *(unsigned *)left,
&r = *(unsigned *)right;
return l < r ? -1 : l > r ? 1 : 0;
} );
};
auto cppSort = []( unsigned *begin, unsigned *end )
{
sort( begin, end );
};
double tC, tCpp, rel;
for( size_t s = 2, rounds; s <= V_MAX; s *= 2 )
rounds = (V_MAX_ROUNDS / s) * V_MAX_ROUNDS,
tC = timedSort( cSort, s, rounds ),
tCpp = timedSort( cppSort, s, rounds ),
rel = tC / tCpp * 100.0,
cout << s << ": " << (int)(rel + 0.5) << "%" << endl;
}

These are the results on my computer (Ryzen Threadripper 3990X):

2: 279%
4: 303%
8: 531%
16: 611%
32: 543%
64: 627%
128: 536%
256: 481%
512: 402%
1024: 316%
2048: 260%
4096: 230%

Up to 6,3 times faster with less coding !

Re: qsort() vs. std::sort

<t409vf$itu$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21395&group=comp.lang.c#21395

  copy link   Newsgroups: comp.lang.c++ comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c
Subject: Re: qsort() vs. std::sort
Date: Sat, 23 Apr 2022 09:31:13 +0200
Organization: A noiseless patient Spider
Lines: 87
Message-ID: <t409vf$itu$1@dont-email.me>
References: <t4092b$des$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 23 Apr 2022 07:30:55 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f8878cd36041ec7a8da220aa265ccafb";
logging-data="19390"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uSNR4ye4CuqwfKPpMXkh+6Wme7oDNOZI="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:swjbGaWRnoYjAYHC1y1docDXLxY=
In-Reply-To: <t4092b$des$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Sat, 23 Apr 2022 07:31 UTC

Am 23.04.2022 um 09:15 schrieb Bonita Montero:
> I just measured how superior C++'s std::sort is over C's qsort:
>
> #include <iostream>
> #include <vector>
> #include <random>
> #include <chrono>
> #include <cstdlib>
> #include <algorithm>
> #include <memory>
> #include <atomic>
>
> using namespace std;
> using namespace chrono;
>
> atomic_uint auNonOptimize;
>
> int main()
> {
>     constexpr size_t
>         V_MAX = 0x1000,
>         V_MAX_ROUNDS = 0x4000;
>     vector<unsigned>
>         vu( V_MAX, 0 ),
>         vuSort( V_MAX, 0 );
>     mt19937_64 mt;
>     uniform_int_distribution<unsigned> uid( 0, -1 );
>     for( unsigned &u : vu )
>         u = uid( mt );
>     auto timedSort = [&]<typename SortFn>( SortFn sortFn, size_t n,
> size_t rounds ) -> double
>         requires requires( SortFn sortFn, unsigned *p ) { { sortFn( p,
> p ) }; }
>     {
>         auto start = high_resolution_clock::now();
>         unsigned sum = 0;
>         while( rounds-- )
>             copy( vu.begin(), vu.begin() + n, vuSort.begin() ),
>             sortFn( to_address( vuSort.begin() ), to_address(
> vuSort.begin() ) + n ),
>             sum += vu[0];
sum += vusort[0];
>         ::auNonOptimize = sum;
>         return (int64_t)duration_cast<nanoseconds>(
> high_resolution_clock::now() - start ).count() / 1.0e9;
>     };
>     auto cSort = []( unsigned *begin, unsigned *end )
>     {
>         qsort( begin, end - begin, sizeof(unsigned),
>             []( void const *left, void const *right ) -> int
>             {
>                 unsigned
>                     &l = *(unsigned *)left,
>                     &r = *(unsigned *)right;
>                 return l < r ? -1 : l > r ? 1 : 0;
>             } );
>     };
>     auto cppSort = []( unsigned *begin, unsigned *end )
>     {
>         sort( begin, end );
>     };
>     double tC, tCpp, rel;
>     for( size_t s = 2, rounds; s <= V_MAX; s *= 2 )
>         rounds = (V_MAX_ROUNDS / s) * V_MAX_ROUNDS,
>         tC = timedSort( cSort, s, rounds ),
>         tCpp = timedSort( cppSort, s, rounds ),
>         rel = tC / tCpp * 100.0,
>         cout << s << ": " << (int)(rel + 0.5) << "%" << endl;
> }
>
> These are the results on my computer (Ryzen Threadripper 3990X):
>
> 2: 279%
> 4: 303%
> 8: 531%
> 16: 611%
> 32: 543%
> 64: 627%
> 128: 536%
> 256: 481%
> 512: 402%
> 1024: 316%
> 2048: 260%
> 4096: 230%
>
> Up to 6,3 times faster with less coding !

Re: qsort() vs. std::sort

<bb6cb651-6c86-49cc-bdee-83623c2bfb83n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21396&group=comp.lang.c#21396

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:23cc:b0:44f:4974:4c1c with SMTP id hr12-20020a05621423cc00b0044f49744c1cmr6511348qvb.116.1650710150823;
Sat, 23 Apr 2022 03:35:50 -0700 (PDT)
X-Received: by 2002:a05:6214:27ce:b0:449:9753:ee08 with SMTP id
ge14-20020a05621427ce00b004499753ee08mr6518107qvb.13.1650710150606; Sat, 23
Apr 2022 03:35:50 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 23 Apr 2022 03:35:50 -0700 (PDT)
In-Reply-To: <t4092b$des$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.243; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.243
References: <t4092b$des$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <bb6cb651-6c86-49cc-bdee-83623c2bfb83n@googlegroups.com>
Subject: Re: qsort() vs. std::sort
From: profesor...@gmail.com (fir)
Injection-Date: Sat, 23 Apr 2022 10:35:50 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 100
 by: fir - Sat, 23 Apr 2022 10:35 UTC

sobota, 23 kwietnia 2022 o 09:15:38 UTC+2 Bonita Montero napisał(a):
> I just measured how superior C++'s std::sort is over C's qsort:
>
> #include <iostream>
> #include <vector>
> #include <random>
> #include <chrono>
> #include <cstdlib>
> #include <algorithm>
> #include <memory>
> #include <atomic>
>
> using namespace std;
> using namespace chrono;
>
> atomic_uint auNonOptimize;
>
> int main()
> {
> constexpr size_t
> V_MAX = 0x1000,
> V_MAX_ROUNDS = 0x4000;
> vector<unsigned>
> vu( V_MAX, 0 ),
> vuSort( V_MAX, 0 );
> mt19937_64 mt;
> uniform_int_distribution<unsigned> uid( 0, -1 );
> for( unsigned &u : vu )
> u = uid( mt );
> auto timedSort = [&]<typename SortFn>( SortFn sortFn, size_t n, size_t
> rounds ) -> double
> requires requires( SortFn sortFn, unsigned *p ) { { sortFn( p, p ) }; }
> {
> auto start = high_resolution_clock::now();
> unsigned sum = 0;
> while( rounds-- )
> copy( vu.begin(), vu.begin() + n, vuSort.begin() ),
> sortFn( to_address( vuSort.begin() ), to_address( vuSort.begin() ) + n ),
> sum += vu[0];
> ::auNonOptimize = sum;
> return (int64_t)duration_cast<nanoseconds>(
> high_resolution_clock::now() - start ).count() / 1.0e9;
> };
> auto cSort = []( unsigned *begin, unsigned *end )
> {
> qsort( begin, end - begin, sizeof(unsigned),
> []( void const *left, void const *right ) -> int
> {
> unsigned
> &l = *(unsigned *)left,
> &r = *(unsigned *)right;
> return l < r ? -1 : l > r ? 1 : 0;
> } );
> };
> auto cppSort = []( unsigned *begin, unsigned *end )
> {
> sort( begin, end );
> };
> double tC, tCpp, rel;
> for( size_t s = 2, rounds; s <= V_MAX; s *= 2 )
> rounds = (V_MAX_ROUNDS / s) * V_MAX_ROUNDS,
> tC = timedSort( cSort, s, rounds ),
> tCpp = timedSort( cppSort, s, rounds ),
> rel = tC / tCpp * 100.0,
> cout << s << ": " << (int)(rel + 0.5) << "%" << endl;
> }
>
> These are the results on my computer (Ryzen Threadripper 3990X):
>
> 2: 279%
> 4: 303%
> 8: 531%
> 16: 611%
> 32: 543%
> 64: 627%
> 128: 536%
> 256: 481%
> 512: 402%
> 1024: 316%
> 2048: 260%
> 4096: 230%
>
> Up to 6,3 times faster with less coding !

try radix sort, i once measured that things (on 10-yo old machine) and testing sorting 1MB of integers and remember c quicksort (like that from wikipedia) have 150 ms , c++ std:sort 111 ms,
c insertsort 111 ms also or 2 ms worse something like that, c-radix sort (taken from stack overflow) 17 ms beating c++:stdsort more than 3 times
for more details search "quicksort" in this group (the search in google groups work)..i only remember vagually that some version of quicksort i tested had some error which i made by trials of revritting but i dont know which one - the conclusions i remember were btw that quicksort is not so good people belive, insertsort was better afair and especially radix sort smashes c++ sort in pieces
(hovever this research then was tiring and i dont want to go beck into it..untill i would really needet it again)

Re: qsort() vs. std::sort

<ab619a2f-c53e-4c3b-a5b4-41a87716a4ben@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21398&group=comp.lang.c#21398

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:5a89:0:b0:2f3:5ab1:3e4f with SMTP id c9-20020ac85a89000000b002f35ab13e4fmr4347057qtc.528.1650710323277;
Sat, 23 Apr 2022 03:38:43 -0700 (PDT)
X-Received: by 2002:a05:6214:23ce:b0:441:8296:a11e with SMTP id
hr14-20020a05621423ce00b004418296a11emr6693993qvb.16.1650710323068; Sat, 23
Apr 2022 03:38:43 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 23 Apr 2022 03:38:42 -0700 (PDT)
In-Reply-To: <bb6cb651-6c86-49cc-bdee-83623c2bfb83n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.243; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.243
References: <t4092b$des$1@dont-email.me> <bb6cb651-6c86-49cc-bdee-83623c2bfb83n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ab619a2f-c53e-4c3b-a5b4-41a87716a4ben@googlegroups.com>
Subject: Re: qsort() vs. std::sort
From: profesor...@gmail.com (fir)
Injection-Date: Sat, 23 Apr 2022 10:38:43 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 17
 by: fir - Sat, 23 Apr 2022 10:38 UTC

> >
> > Up to 6,3 times faster with less coding !
> try radix sort, i once measured that things (on 10-yo old machine) and testing sorting 1MB of integers and remember c quicksort (like that from wikipedia) have 150 ms , c++ std:sort 111 ms,
> c insertsort 111 ms also or 2 ms worse something like that, c-radix sort (taken from stack overflow) 17 ms beating c++:stdsort more than 3 times

sorry mnistype i wanted to type 27 ms (not 17 ms)

> for more details search "quicksort" in this group (the search in google groups work)..i only remember vagually that some version of quicksort i tested had some error which i made by trials of revritting but i dont know which one - the conclusions i remember were btw that quicksort is not so good people belive, insertsort was better afair and especially radix sort smashes c++ sort in pieces
> (hovever this research then was tiring and i dont want to go beck into it...untill i would really needet it again)

Re: qsort() vs. std::sort

<39c51147-565d-4f1b-b4ac-b348c96bd127n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21401&group=comp.lang.c#21401

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:4113:b0:69f:10ee:bd3d with SMTP id j19-20020a05620a411300b0069f10eebd3dmr5043277qko.631.1650711627082;
Sat, 23 Apr 2022 04:00:27 -0700 (PDT)
X-Received: by 2002:a05:622a:91:b0:2f1:f9ce:405a with SMTP id
o17-20020a05622a009100b002f1f9ce405amr6097091qtw.194.1650711626890; Sat, 23
Apr 2022 04:00:26 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 23 Apr 2022 04:00:26 -0700 (PDT)
In-Reply-To: <t4092b$des$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:c1f9:a253:ca5e:a7eb;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:c1f9:a253:ca5e:a7eb
References: <t4092b$des$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <39c51147-565d-4f1b-b4ac-b348c96bd127n@googlegroups.com>
Subject: Re: qsort() vs. std::sort
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Sat, 23 Apr 2022 11:00:27 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 85
 by: Malcolm McLean - Sat, 23 Apr 2022 11:00 UTC

On Saturday, 23 April 2022 at 08:15:38 UTC+1, Bonita Montero wrote:
> I just measured how superior C++'s std::sort is over C's qsort:
>
> #include <iostream>
> #include <vector>
> #include <random>
> #include <chrono>
> #include <cstdlib>
> #include <algorithm>
> #include <memory>
> #include <atomic>
>
> using namespace std;
> using namespace chrono;
>
> atomic_uint auNonOptimize;
>
> int main()
> {
> constexpr size_t
> V_MAX = 0x1000,
> V_MAX_ROUNDS = 0x4000;
> vector<unsigned>
> vu( V_MAX, 0 ),
> vuSort( V_MAX, 0 );
> mt19937_64 mt;
> uniform_int_distribution<unsigned> uid( 0, -1 );
> for( unsigned &u : vu )
> u = uid( mt );
> auto timedSort = [&]<typename SortFn>( SortFn sortFn, size_t n, size_t
> rounds ) -> double
> requires requires( SortFn sortFn, unsigned *p ) { { sortFn( p, p ) }; }
> {
> auto start = high_resolution_clock::now();
> unsigned sum = 0;
> while( rounds-- )
> copy( vu.begin(), vu.begin() + n, vuSort.begin() ),
> sortFn( to_address( vuSort.begin() ), to_address( vuSort.begin() ) + n ),
> sum += vu[0];
> ::auNonOptimize = sum;
> return (int64_t)duration_cast<nanoseconds>(
> high_resolution_clock::now() - start ).count() / 1.0e9;
> };
> auto cSort = []( unsigned *begin, unsigned *end )
> {
> qsort( begin, end - begin, sizeof(unsigned),
> []( void const *left, void const *right ) -> int
> {
> unsigned
> &l = *(unsigned *)left,
> &r = *(unsigned *)right;
> return l < r ? -1 : l > r ? 1 : 0;
> } );
> };
> auto cppSort = []( unsigned *begin, unsigned *end )
> {
> sort( begin, end );
> };
> double tC, tCpp, rel;
> for( size_t s = 2, rounds; s <= V_MAX; s *= 2 )
> rounds = (V_MAX_ROUNDS / s) * V_MAX_ROUNDS,
> tC = timedSort( cSort, s, rounds ),
> tCpp = timedSort( cppSort, s, rounds ),
> rel = tC / tCpp * 100.0,
> cout << s << ": " << (int)(rel + 0.5) << "%" << endl;
> }
>
> These are the results on my computer (Ryzen Threadripper 3990X):
>
> 2: 279%
> 4: 303%
> 8: 531%
> 16: 611%
> 32: 543%
> 64: 627%
> 128: 536%
> 256: 481%
> 512: 402%
> 1024: 316%
> 2048: 260%
> 4096: 230%
>
> Up to 6,3 times faster with less coding !
>
But the advantage is tailing off as N gets larger. Probably because the runtime starts to be
dominated by cache misses rather than by overhead in the comparison function.

Re: qsort() vs. std::sort

<d4a13d5f-52f9-4ec8-9f3b-dd9702ed75e0n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21402&group=comp.lang.c#21402

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:5b81:0:b0:456:2c7f:97ab with SMTP id 1-20020ad45b81000000b004562c7f97abmr1410197qvp.71.1650712082195;
Sat, 23 Apr 2022 04:08:02 -0700 (PDT)
X-Received: by 2002:a05:620a:219d:b0:69e:8ab0:dd22 with SMTP id
g29-20020a05620a219d00b0069e8ab0dd22mr5295287qka.33.1650712081981; Sat, 23
Apr 2022 04:08:01 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 23 Apr 2022 04:08:01 -0700 (PDT)
In-Reply-To: <39c51147-565d-4f1b-b4ac-b348c96bd127n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.243; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.243
References: <t4092b$des$1@dont-email.me> <39c51147-565d-4f1b-b4ac-b348c96bd127n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d4a13d5f-52f9-4ec8-9f3b-dd9702ed75e0n@googlegroups.com>
Subject: Re: qsort() vs. std::sort
From: profesor...@gmail.com (fir)
Injection-Date: Sat, 23 Apr 2022 11:08:02 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 105
 by: fir - Sat, 23 Apr 2022 11:08 UTC

sobota, 23 kwietnia 2022 o 13:00:33 UTC+2 Malcolm McLean napisał(a):
> On Saturday, 23 April 2022 at 08:15:38 UTC+1, Bonita Montero wrote:
> > I just measured how superior C++'s std::sort is over C's qsort:
> >
> > #include <iostream>
> > #include <vector>
> > #include <random>
> > #include <chrono>
> > #include <cstdlib>
> > #include <algorithm>
> > #include <memory>
> > #include <atomic>
> >
> > using namespace std;
> > using namespace chrono;
> >
> > atomic_uint auNonOptimize;
> >
> > int main()
> > {
> > constexpr size_t
> > V_MAX = 0x1000,
> > V_MAX_ROUNDS = 0x4000;
> > vector<unsigned>
> > vu( V_MAX, 0 ),
> > vuSort( V_MAX, 0 );
> > mt19937_64 mt;
> > uniform_int_distribution<unsigned> uid( 0, -1 );
> > for( unsigned &u : vu )
> > u = uid( mt );
> > auto timedSort = [&]<typename SortFn>( SortFn sortFn, size_t n, size_t
> > rounds ) -> double
> > requires requires( SortFn sortFn, unsigned *p ) { { sortFn( p, p ) }; }
> > {
> > auto start = high_resolution_clock::now();
> > unsigned sum = 0;
> > while( rounds-- )
> > copy( vu.begin(), vu.begin() + n, vuSort.begin() ),
> > sortFn( to_address( vuSort.begin() ), to_address( vuSort.begin() ) + n ),
> > sum += vu[0];
> > ::auNonOptimize = sum;
> > return (int64_t)duration_cast<nanoseconds>(
> > high_resolution_clock::now() - start ).count() / 1.0e9;
> > };
> > auto cSort = []( unsigned *begin, unsigned *end )
> > {
> > qsort( begin, end - begin, sizeof(unsigned),
> > []( void const *left, void const *right ) -> int
> > {
> > unsigned
> > &l = *(unsigned *)left,
> > &r = *(unsigned *)right;
> > return l < r ? -1 : l > r ? 1 : 0;
> > } );
> > };
> > auto cppSort = []( unsigned *begin, unsigned *end )
> > {
> > sort( begin, end );
> > };
> > double tC, tCpp, rel;
> > for( size_t s = 2, rounds; s <= V_MAX; s *= 2 )
> > rounds = (V_MAX_ROUNDS / s) * V_MAX_ROUNDS,
> > tC = timedSort( cSort, s, rounds ),
> > tCpp = timedSort( cppSort, s, rounds ),
> > rel = tC / tCpp * 100.0,
> > cout << s << ": " << (int)(rel + 0.5) << "%" << endl;
> > }
> >
> > These are the results on my computer (Ryzen Threadripper 3990X):
> >
> > 2: 279%
> > 4: 303%
> > 8: 531%
> > 16: 611%
> > 32: 543%
> > 64: 627%
> > 128: 536%
> > 256: 481%
> > 512: 402%
> > 1024: 316%
> > 2048: 260%
> > 4096: 230%
> >
> > Up to 6,3 times faster with less coding !
> >
> But the advantage is tailing off as N gets larger. Probably because the runtime starts to be
> dominated by cache misses rather than by overhead in the comparison function.

nobody should compare sorting to the qsort that probably comes from 80-ties or something, this
passing this 'predicates' as callbacs (if this predicate goiod word ) is obvious this is not effective

more surprising for me was that c++ ponys clain std::sort is ofc best, thousands od c++ pane care for that and simple routine you cpy and paste beats it 3-4 times (which is very notable)

but i could treat it like old-school anecdote as participating in that pony vars is generally not the best thing man can do imo (though defending C its ok though)

Re: qsort() vs. std::sort

<t40opq$tqb$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21404&group=comp.lang.c#21404

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: qsort() vs. std::sort
Date: Sat, 23 Apr 2022 13:44:13 +0200
Organization: A noiseless patient Spider
Lines: 89
Message-ID: <t40opq$tqb$1@dont-email.me>
References: <t4092b$des$1@dont-email.me>
<39c51147-565d-4f1b-b4ac-b348c96bd127n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 23 Apr 2022 11:43:54 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f8878cd36041ec7a8da220aa265ccafb";
logging-data="30539"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GeFbAHCShOt7UPdnauQGBbszQLFwppG4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:OWOS6f98ZtsqqChS/CNqfpwKIlg=
In-Reply-To: <39c51147-565d-4f1b-b4ac-b348c96bd127n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Sat, 23 Apr 2022 11:44 UTC

Am 23.04.2022 um 13:00 schrieb Malcolm McLean:
> On Saturday, 23 April 2022 at 08:15:38 UTC+1, Bonita Montero wrote:
>> I just measured how superior C++'s std::sort is over C's qsort:
>>
>> #include <iostream>
>> #include <vector>
>> #include <random>
>> #include <chrono>
>> #include <cstdlib>
>> #include <algorithm>
>> #include <memory>
>> #include <atomic>
>>
>> using namespace std;
>> using namespace chrono;
>>
>> atomic_uint auNonOptimize;
>>
>> int main()
>> {
>> constexpr size_t
>> V_MAX = 0x1000,
>> V_MAX_ROUNDS = 0x4000;
>> vector<unsigned>
>> vu( V_MAX, 0 ),
>> vuSort( V_MAX, 0 );
>> mt19937_64 mt;
>> uniform_int_distribution<unsigned> uid( 0, -1 );
>> for( unsigned &u : vu )
>> u = uid( mt );
>> auto timedSort = [&]<typename SortFn>( SortFn sortFn, size_t n, size_t
>> rounds ) -> double
>> requires requires( SortFn sortFn, unsigned *p ) { { sortFn( p, p ) }; }
>> {
>> auto start = high_resolution_clock::now();
>> unsigned sum = 0;
>> while( rounds-- )
>> copy( vu.begin(), vu.begin() + n, vuSort.begin() ),
>> sortFn( to_address( vuSort.begin() ), to_address( vuSort.begin() ) + n ),
>> sum += vu[0];
>> ::auNonOptimize = sum;
>> return (int64_t)duration_cast<nanoseconds>(
>> high_resolution_clock::now() - start ).count() / 1.0e9;
>> };
>> auto cSort = []( unsigned *begin, unsigned *end )
>> {
>> qsort( begin, end - begin, sizeof(unsigned),
>> []( void const *left, void const *right ) -> int
>> {
>> unsigned
>> &l = *(unsigned *)left,
>> &r = *(unsigned *)right;
>> return l < r ? -1 : l > r ? 1 : 0;
>> } );
>> };
>> auto cppSort = []( unsigned *begin, unsigned *end )
>> {
>> sort( begin, end );
>> };
>> double tC, tCpp, rel;
>> for( size_t s = 2, rounds; s <= V_MAX; s *= 2 )
>> rounds = (V_MAX_ROUNDS / s) * V_MAX_ROUNDS,
>> tC = timedSort( cSort, s, rounds ),
>> tCpp = timedSort( cppSort, s, rounds ),
>> rel = tC / tCpp * 100.0,
>> cout << s << ": " << (int)(rel + 0.5) << "%" << endl;
>> }
>>
>> These are the results on my computer (Ryzen Threadripper 3990X):
>>
>> 2: 279%
>> 4: 303%
>> 8: 531%
>> 16: 611%
>> 32: 543%
>> 64: 627%
>> 128: 536%
>> 256: 481%
>> 512: 402%
>> 1024: 316%
>> 2048: 260%
>> 4096: 230%
>>
>> Up to 6,3 times faster with less coding !
>>
> But the advantage is tailing off as N gets larger. Probably because the runtime starts to be
> dominated by cache misses ...

Maybe you're right.

Re: qsort() vs. std::sort

<t40rkb$k2k$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21408&group=comp.lang.c#21408

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: qsort() vs. std::sort
Date: Sat, 23 Apr 2022 14:32:30 +0200
Organization: A noiseless patient Spider
Lines: 124
Message-ID: <t40rkb$k2k$1@dont-email.me>
References: <t4092b$des$1@dont-email.me>
<bb6cb651-6c86-49cc-bdee-83623c2bfb83n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 23 Apr 2022 12:32:11 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f8878cd36041ec7a8da220aa265ccafb";
logging-data="20564"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Vf2z4GDqIYGebKTgpbFcs7mekX2l3bHI="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:DkK6o0MuQVKTuq73YcOr/d5u1hg=
In-Reply-To: <bb6cb651-6c86-49cc-bdee-83623c2bfb83n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Sat, 23 Apr 2022 12:32 UTC

Radix-sort is significantly slower because of the memory-allocations:

#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <cstdlib>
#include <algorithm>
#include <memory>
#include <atomic>
#include "radix_sort.h"

using namespace std;
using namespace chrono;

atomic_uint auNonOptimize;

int main()
{ constexpr size_t
V_MAX = 0x1000,
V_MAX_ROUNDS = 0x1000;
using vu_t = vector<unsigned>;
using vu_it = vu_t::iterator;
vu_t
vu( V_MAX, 0 ),
vuSort( V_MAX, 0 );
mt19937_64 mt;
uniform_int_distribution<unsigned> uid( 0, -1 );
for( unsigned &u : vu )
u = uid( mt );
auto timedSort = [&]<typename SortFn>( SortFn sortFn, size_t n, size_t
rounds ) -> double
requires requires( SortFn sortFn, vu_it it ) { { sortFn( it, it ) }; }
{
auto start = high_resolution_clock::now();
unsigned sum = 0;
while( rounds-- )
copy( vu.begin(), vu.begin() + n, vuSort.begin() ),
sortFn( vuSort.begin(), vuSort.begin() + n ),
sum += vuSort[0];
::auNonOptimize = sum;
return (int64_t)duration_cast<nanoseconds>(
high_resolution_clock::now() - start ).count() / 1.0e9;
};
auto cSort = []( vu_it begin, vu_it end )
{
qsort( to_address( begin ), end - begin, sizeof(unsigned),
[]( void const *left, void const *right ) -> int
{
unsigned
&l = *(unsigned *)left,
&r = *(unsigned *)right;
return l < r ? -1 : l > r ? 1 : 0;
} );
};
auto cppSort = []( vu_it begin, vu_it end )
{
sort( begin, end );
};
auto rdxSort = []( vu_it begin, vu_it end )
{
radix_sort( begin, end );
};
auto rel = []( double tC, double tRel ) -> int { return (int)(tC /
tRel * 100.0 + 0.5); };
double tC, tCpp, tRdx;
for( size_t s = 2, rounds; s <= V_MAX; s *= 2 )
rounds = (V_MAX_ROUNDS / s) * V_MAX_ROUNDS,
tC = timedSort( cSort, s, rounds ),
tCpp = timedSort( cppSort, s, rounds ),
tRdx = timedSort( rdxSort, s, rounds ),
cout << s << ": " << rel( tC, tCpp ) << "%, " << rel( tC, tRdx ) <<
"%" << endl;
}

// radix_sort.h:

#pragma once
#include <iterator>
#include <vector>
#include <array>
#include <climits>
#include <algorithm>
#include <type_traits>

template<std::forward_iterator ForwardIt>
requires std::is_integral_v<typename
std::iterator_traits<ForwardIt>::value_type>
void radix_sort( ForwardIt begin, ForwardIt end )
{ using namespace std;
if( begin == end )
return;
using elem_t = typename iterator_traits<ForwardIt>::value_type;
array<vector<elem_t>, 2> partitions;
for( unsigned bit = 0; bit != sizeof(elem_t) * CHAR_BIT; ++bit )
{
for( ForwardIt it = begin; it != end; ++it )
partitions[(*it >> bit) & 1].push_back( *it );
auto end0 = copy( partitions[0].begin(), partitions[0].end(), begin );
copy( partitions[1].begin(), partitions[1].end(), end0 );
partitions[0].clear();
partitions[1].clear();
}
}

2: 120%, 1%
4: 210%, 2%
8: 498%, 5%
16: 561%, 10%
32: 433%, 13%
64: 535%, 26%
128: 511%, 37%
256: 448%, 39%
512: 400%, 47%
1024: 321%, 55%
2048: 258%, 61%
4096: 225%, 68%

The first number is std::sort vs. qsort, the second number
is radix_sort vs. qsort. I think I could speed up radix_sort
significantly by allocating the buffer on the stack with alloca().

Re: qsort() vs. std::sort

<a7c6a83b-6536-4c2c-b362-836860d2bd86n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21410&group=comp.lang.c#21410

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:c23:b0:446:64d2:260c with SMTP id a3-20020a0562140c2300b0044664d2260cmr6746923qvd.26.1650717671155;
Sat, 23 Apr 2022 05:41:11 -0700 (PDT)
X-Received: by 2002:ad4:594c:0:b0:449:95d6:d715 with SMTP id
eo12-20020ad4594c000000b0044995d6d715mr6867219qvb.115.1650717671024; Sat, 23
Apr 2022 05:41:11 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 23 Apr 2022 05:41:10 -0700 (PDT)
In-Reply-To: <t40rkb$k2k$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.157; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.157
References: <t4092b$des$1@dont-email.me> <bb6cb651-6c86-49cc-bdee-83623c2bfb83n@googlegroups.com>
<t40rkb$k2k$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a7c6a83b-6536-4c2c-b362-836860d2bd86n@googlegroups.com>
Subject: Re: qsort() vs. std::sort
From: profesor...@gmail.com (fir)
Injection-Date: Sat, 23 Apr 2022 12:41:11 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 33
 by: fir - Sat, 23 Apr 2022 12:41 UTC

sobota, 23 kwietnia 2022 o 14:32:24 UTC+2 Bonita Montero napisał(a):
> Radix-sort is significantly slower because of the memory-allocations:
> #include <iostream>
> #include <vector>
> }
>
> 2: 120%, 1%
> 4: 210%, 2%
> 8: 498%, 5%
> 16: 561%, 10%
> 32: 433%, 13%
> 64: 535%, 26%
> 128: 511%, 37%
> 256: 448%, 39%
> 512: 400%, 47%
> 1024: 321%, 55%
> 2048: 258%, 61%
> 4096: 225%, 68%
>
> The first number is std::sort vs. qsort, the second number
> is radix_sort vs. qsort. I think I could speed up radix_sort
> significantly by allocating the buffer on the stack with alloca().

you better give miliseconds - like those numbers i gave
the radix sort i took is here in this thread

https://groups.google.com/g/comp.lang.c/c/I4lJSh45EJ0/m/ezGwgnvWDgAJ

hovever i dont want to get back to this now, this is low lewel of coding and optimisation
and this is wearing, note any time im ready to get down and involved, esp as those algorithms are hard to understand to the bone and being able to do it own way

Re: qsort() vs. std::sort

<t40spq$sbm$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21411&group=comp.lang.c#21411

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: qsort() vs. std::sort
Date: Sat, 23 Apr 2022 14:52:29 +0200
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <t40spq$sbm$1@dont-email.me>
References: <t4092b$des$1@dont-email.me>
<bb6cb651-6c86-49cc-bdee-83623c2bfb83n@googlegroups.com>
<t40rkb$k2k$1@dont-email.me>
<a7c6a83b-6536-4c2c-b362-836860d2bd86n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 23 Apr 2022 12:52:10 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f8878cd36041ec7a8da220aa265ccafb";
logging-data="29046"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fJu/C1nZ0vHUaCqxNAImaE4eE+xchQFo="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:j1khOUBDdXAgFQHCRs7568WXJUQ=
In-Reply-To: <a7c6a83b-6536-4c2c-b362-836860d2bd86n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Sat, 23 Apr 2022 12:52 UTC

Am 23.04.2022 um 14:41 schrieb fir:
> sobota, 23 kwietnia 2022 o 14:32:24 UTC+2 Bonita Montero napisał(a):
>> Radix-sort is significantly slower because of the memory-allocations:
>> #include <iostream>
>> #include <vector>
>> }
>>
>> 2: 120%, 1%
>> 4: 210%, 2%
>> 8: 498%, 5%
>> 16: 561%, 10%
>> 32: 433%, 13%
>> 64: 535%, 26%
>> 128: 511%, 37%
>> 256: 448%, 39%
>> 512: 400%, 47%
>> 1024: 321%, 55%
>> 2048: 258%, 61%
>> 4096: 225%, 68%
>>
>> The first number is std::sort vs. qsort, the second number
>> is radix_sort vs. qsort. I think I could speed up radix_sort
>> significantly by allocating the buffer on the stack with alloca().
>
> you better give miliseconds - like those numbers i gave
> the radix sort i took is here in this thread
>
> https://groups.google.com/g/comp.lang.c/c/I4lJSh45EJ0/m/ezGwgnvWDgAJ
>
> hovever i dont want to get back to this now, this is low lewel of coding and optimisation
> and this is wearing, note any time im ready to get down and involved, esp as those algorithms are hard to understand to the bone and being able to do it own way

I output the relative speed vs. the C's qsort.
There's nothing to optimize further with my radix_sort
except from the memory-allocations.

Re: qsort() vs. std::sort

<edb2f929-f576-4870-9bf9-4a245dea5fd3n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21412&group=comp.lang.c#21412

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:18a3:b0:2f1:f958:38cc with SMTP id v35-20020a05622a18a300b002f1f95838ccmr6374613qtc.171.1650718747476;
Sat, 23 Apr 2022 05:59:07 -0700 (PDT)
X-Received: by 2002:a05:622a:6114:b0:2f0:ffc8:53f8 with SMTP id
hg20-20020a05622a611400b002f0ffc853f8mr6350609qtb.681.1650718747322; Sat, 23
Apr 2022 05:59:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 23 Apr 2022 05:59:07 -0700 (PDT)
In-Reply-To: <t40spq$sbm$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.101; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.101
References: <t4092b$des$1@dont-email.me> <bb6cb651-6c86-49cc-bdee-83623c2bfb83n@googlegroups.com>
<t40rkb$k2k$1@dont-email.me> <a7c6a83b-6536-4c2c-b362-836860d2bd86n@googlegroups.com>
<t40spq$sbm$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <edb2f929-f576-4870-9bf9-4a245dea5fd3n@googlegroups.com>
Subject: Re: qsort() vs. std::sort
From: profesor...@gmail.com (fir)
Injection-Date: Sat, 23 Apr 2022 12:59:07 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 57
 by: fir - Sat, 23 Apr 2022 12:59 UTC

sobota, 23 kwietnia 2022 o 14:52:23 UTC+2 Bonita Montero napisał(a):
> Am 23.04.2022 um 14:41 schrieb fir:
> > sobota, 23 kwietnia 2022 o 14:32:24 UTC+2 Bonita Montero napisał(a):
> >> Radix-sort is significantly slower because of the memory-allocations:
> >> #include <iostream>
> >> #include <vector>
> >> }
> >>
> >> 2: 120%, 1%
> >> 4: 210%, 2%
> >> 8: 498%, 5%
> >> 16: 561%, 10%
> >> 32: 433%, 13%
> >> 64: 535%, 26%
> >> 128: 511%, 37%
> >> 256: 448%, 39%
> >> 512: 400%, 47%
> >> 1024: 321%, 55%
> >> 2048: 258%, 61%
> >> 4096: 225%, 68%
> >>
> >> The first number is std::sort vs. qsort, the second number
> >> is radix_sort vs. qsort. I think I could speed up radix_sort
> >> significantly by allocating the buffer on the stack with alloca().
> >
> > you better give miliseconds - like those numbers i gave
> > the radix sort i took is here in this thread
> >
> > https://groups.google.com/g/comp.lang.c/c/I4lJSh45EJ0/m/ezGwgnvWDgAJ
> >
> > hovever i dont want to get back to this now, this is low lewel of coding and optimisation
> > and this is wearing, note any time im ready to get down and involved, esp as those algorithms are hard to understand to the bone and being able to do it own way
> I output the relative speed vs. the C's qsort.
> There's nothing to optimize further with my radix_sort
> except from the memory-allocations.

qsort is nothing to compare as it calls those predicates by pointers (if im not wrong pasing its arguments back and forth on stack, maybe in case of x64 its not but this is overally stupid, besides quicksort si probably not the good alghoritm.. as far as i remember needed to improve it by insertion sort for short partitons etc... i dont want top get back to that)

you should giove miliseconds as it is reall life information which is kinda 'physical thus most interesting and most infromative.. if also could compare this result with rest of the world not some % relative to one of your code to other...giving miliseconds (and soem information on machine and settings) gave much more sense

if you want go pro on sorting handmade solutions will always be fastests imo becouse you may use soem assumptions on what your data and what you need which generall cal cant use... goint this specific way is most fun though its most hardcore too

Re: qsort() vs. std::sort

<t40thm$2nf$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21413&group=comp.lang.c#21413

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: qsort() vs. std::sort
Date: Sat, 23 Apr 2022 15:05:13 +0200
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <t40thm$2nf$1@dont-email.me>
References: <t4092b$des$1@dont-email.me>
<bb6cb651-6c86-49cc-bdee-83623c2bfb83n@googlegroups.com>
<t40rkb$k2k$1@dont-email.me>
<a7c6a83b-6536-4c2c-b362-836860d2bd86n@googlegroups.com>
<t40spq$sbm$1@dont-email.me>
<edb2f929-f576-4870-9bf9-4a245dea5fd3n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 23 Apr 2022 13:04:54 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f8878cd36041ec7a8da220aa265ccafb";
logging-data="2799"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+mP/KeAgGcD3azH/eJma6J/qz3X3As1oM="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:kL3l6BD6RntbqP7ig2AMQ8hK0aQ=
In-Reply-To: <edb2f929-f576-4870-9bf9-4a245dea5fd3n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Sat, 23 Apr 2022 13:05 UTC

Am 23.04.2022 um 14:59 schrieb fir:
> sobota, 23 kwietnia 2022 o 14:52:23 UTC+2 Bonita Montero napisał(a):
>> Am 23.04.2022 um 14:41 schrieb fir:
>>> sobota, 23 kwietnia 2022 o 14:32:24 UTC+2 Bonita Montero napisał(a):
>>>> Radix-sort is significantly slower because of the memory-allocations:
>>>> #include <iostream>
>>>> #include <vector>
>>>> }
>>>>
>>>> 2: 120%, 1%
>>>> 4: 210%, 2%
>>>> 8: 498%, 5%
>>>> 16: 561%, 10%
>>>> 32: 433%, 13%
>>>> 64: 535%, 26%
>>>> 128: 511%, 37%
>>>> 256: 448%, 39%
>>>> 512: 400%, 47%
>>>> 1024: 321%, 55%
>>>> 2048: 258%, 61%
>>>> 4096: 225%, 68%
>>>>
>>>> The first number is std::sort vs. qsort, the second number
>>>> is radix_sort vs. qsort. I think I could speed up radix_sort
>>>> significantly by allocating the buffer on the stack with alloca().
>>>
>>> you better give miliseconds - like those numbers i gave
>>> the radix sort i took is here in this thread
>>>
>>> https://groups.google.com/g/comp.lang.c/c/I4lJSh45EJ0/m/ezGwgnvWDgAJ
>>>
>>> hovever i dont want to get back to this now, this is low lewel of coding and optimisation
>>> and this is wearing, note any time im ready to get down and involved, esp as those algorithms are hard to understand to the bone and being able to do it own way
>> I output the relative speed vs. the C's qsort.
>> There's nothing to optimize further with my radix_sort
>> except from the memory-allocations.
>
> qsort is nothing to compare as it calls those predicates by pointers ...

It doesn't matter to what I compare, even if I'd compare to bubblesort
since you can compre the relative numbers among each other.

> you should giove miliseconds ...

Irrelevant since I have relative numbers.

Re: qsort() vs. std::sort

<73bca35e-024b-4d22-a745-5e1a1bb44d42n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21414&group=comp.lang.c#21414

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:4104:b0:42c:1db0:da28 with SMTP id kc4-20020a056214410400b0042c1db0da28mr6903698qvb.67.1650721057789;
Sat, 23 Apr 2022 06:37:37 -0700 (PDT)
X-Received: by 2002:a05:622a:14cb:b0:2e1:9fc5:424d with SMTP id
u11-20020a05622a14cb00b002e19fc5424dmr6463356qtx.543.1650721057609; Sat, 23
Apr 2022 06:37:37 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 23 Apr 2022 06:37:37 -0700 (PDT)
In-Reply-To: <t40thm$2nf$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=5.172.255.101; posting-account=Sb6m8goAAABbWsBL7gouk3bfLsuxwMgN
NNTP-Posting-Host: 5.172.255.101
References: <t4092b$des$1@dont-email.me> <bb6cb651-6c86-49cc-bdee-83623c2bfb83n@googlegroups.com>
<t40rkb$k2k$1@dont-email.me> <a7c6a83b-6536-4c2c-b362-836860d2bd86n@googlegroups.com>
<t40spq$sbm$1@dont-email.me> <edb2f929-f576-4870-9bf9-4a245dea5fd3n@googlegroups.com>
<t40thm$2nf$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <73bca35e-024b-4d22-a745-5e1a1bb44d42n@googlegroups.com>
Subject: Re: qsort() vs. std::sort
From: profesor...@gmail.com (fir)
Injection-Date: Sat, 23 Apr 2022 13:37:37 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 8
 by: fir - Sat, 23 Apr 2022 13:37 UTC

sobota, 23 kwietnia 2022 o 15:05:07 UTC+2 Bonita Montero napisał(a):
> Am 23.04.2022 um 14:59 schrieb fir:
>
> > you should giove miliseconds ...
>
> Irrelevant since I have relative numbers.

if you say so....

Re: qsort() vs. std::sort

<t45d5t$1sln$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21448&group=comp.lang.c#21448

  copy link   Newsgroups: comp.lang.c++ comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!NK0c7qMEn6mmBWqphs27pg.user.46.165.242.75.POSTED!not-for-mail
From: nos...@thanks.invalid (Juha Nieminen)
Newsgroups: comp.lang.c++,comp.lang.c
Subject: Re: qsort() vs. std::sort
Date: Mon, 25 Apr 2022 05:56:15 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <t45d5t$1sln$1@gioia.aioe.org>
References: <t4092b$des$1@dont-email.me>
Injection-Info: gioia.aioe.org; logging-data="62135"; posting-host="NK0c7qMEn6mmBWqphs27pg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: tin/2.4.3-20181224 ("Glen Mhor") (UNIX) (Linux/5.10.103-grsec-kapsi (x86_64))
X-Notice: Filtered by postfilter v. 0.9.2
 by: Juha Nieminen - Mon, 25 Apr 2022 05:56 UTC

In comp.lang.c++ Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Up to 6,3 times faster with less coding !

std::sort() benefits a lot from being templated and not requiring callback
functions for comparing elements. (Many of the other operations are also
likely to benefit from directly seeing and handling the element type.)

I am curious to know if qsort() in the major standard library
implementations use the same algorithm as std::sort().

Re: qsort() vs. std::sort

<t45f06$sa1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21449&group=comp.lang.c#21449

  copy link   Newsgroups: comp.lang.c++ comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c
Subject: Re: qsort() vs. std::sort
Date: Mon, 25 Apr 2022 08:27:38 +0200
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <t45f06$sa1$1@dont-email.me>
References: <t4092b$des$1@dont-email.me> <t45d5t$1sln$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Apr 2022 06:27:18 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d4317f93be73149c628069c6284893e3";
logging-data="28993"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19e0R9IKLXDVvALEi2r8doEgDGYUQsfghE="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:znAv9PLeMxjS3jmFH0mw1AOGGUg=
In-Reply-To: <t45d5t$1sln$1@gioia.aioe.org>
Content-Language: de-DE
 by: Bonita Montero - Mon, 25 Apr 2022 06:27 UTC

> std::sort() benefits a lot from being templated and not requiring callback
> functions for comparing elements. ...

Obvious, but you can't guess the actual difference from that.

> I am curious to know if qsort() in the major standard library
> implementations use the same algorithm as std::sort().

Basically yes, bt there are minor improvements over quicksorts which
give a slight improved performance.

Re: qsort() vs. std::sort

<a896c86f-fa1b-4ddb-8218-03b3aa64c3b5n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21450&group=comp.lang.c#21450

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:23cc:b0:44f:4974:4c1c with SMTP id hr12-20020a05621423cc00b0044f49744c1cmr11577968qvb.116.1650869544247;
Sun, 24 Apr 2022 23:52:24 -0700 (PDT)
X-Received: by 2002:a05:622a:189c:b0:2f3:654c:369f with SMTP id
v28-20020a05622a189c00b002f3654c369fmr3251651qtc.50.1650869544049; Sun, 24
Apr 2022 23:52:24 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sun, 24 Apr 2022 23:52:23 -0700 (PDT)
In-Reply-To: <t45d5t$1sln$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=81.143.231.9; posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 81.143.231.9
References: <t4092b$des$1@dont-email.me> <t45d5t$1sln$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a896c86f-fa1b-4ddb-8218-03b3aa64c3b5n@googlegroups.com>
Subject: Re: qsort() vs. std::sort
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Mon, 25 Apr 2022 06:52:24 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 12
 by: Malcolm McLean - Mon, 25 Apr 2022 06:52 UTC

On Monday, 25 April 2022 at 06:56:28 UTC+1, Juha Nieminen wrote:
> In comp.lang.c++ Bonita Montero <Bonita....@gmail.com> wrote:
> > Up to 6,3 times faster with less coding !
> std::sort() benefits a lot from being templated and not requiring callback
> functions for comparing elements. (Many of the other operations are also
> likely to benefit from directly seeing and handling the element type.)
>
It's a general problem with C. Most elements are aligned on hardware
word boundaries. However it is awkward to test for or require that. So you
end up writing inefficient byte-copying loops for portability and convenience.

The other issue is that many compilers won't inline an indirect call, even if
it appears only once and is passed in directly.

Re: qsort() vs. std::sort

<20115f32-0400-4813-8ddb-5be877eba3d6n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21504&group=comp.lang.c#21504

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ae9:de43:0:b0:69f:7585:8276 with SMTP id s64-20020ae9de43000000b0069f75858276mr4687996qkf.706.1651058476583;
Wed, 27 Apr 2022 04:21:16 -0700 (PDT)
X-Received: by 2002:ad4:56e8:0:b0:455:916a:3969 with SMTP id
cr8-20020ad456e8000000b00455916a3969mr16046189qvb.35.1651058476432; Wed, 27
Apr 2022 04:21:16 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Wed, 27 Apr 2022 04:21:16 -0700 (PDT)
In-Reply-To: <t4092b$des$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.248.114; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.248.114
References: <t4092b$des$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <20115f32-0400-4813-8ddb-5be877eba3d6n@googlegroups.com>
Subject: Re: qsort() vs. std::sort
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Wed, 27 Apr 2022 11:21:16 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 88
 by: Thiago Adams - Wed, 27 Apr 2022 11:21 UTC

On Saturday, April 23, 2022 at 4:15:38 AM UTC-3, Bonita Montero wrote:
> I just measured how superior C++'s std::sort is over C's qsort:
>
> #include <iostream>
> #include <vector>
> #include <random>
> #include <chrono>
> #include <cstdlib>
> #include <algorithm>
> #include <memory>
> #include <atomic>
>
> using namespace std;
> using namespace chrono;
>
> atomic_uint auNonOptimize;
>
> int main()
> {
> constexpr size_t
> V_MAX = 0x1000,
> V_MAX_ROUNDS = 0x4000;
> vector<unsigned>
> vu( V_MAX, 0 ),
> vuSort( V_MAX, 0 );
> mt19937_64 mt;
> uniform_int_distribution<unsigned> uid( 0, -1 );
> for( unsigned &u : vu )
> u = uid( mt );
> auto timedSort = [&]<typename SortFn>( SortFn sortFn, size_t n, size_t
> rounds ) -> double
> requires requires( SortFn sortFn, unsigned *p ) { { sortFn( p, p ) }; }
> {
> auto start = high_resolution_clock::now();
> unsigned sum = 0;
> while( rounds-- )
> copy( vu.begin(), vu.begin() + n, vuSort.begin() ),
> sortFn( to_address( vuSort.begin() ), to_address( vuSort.begin() ) + n ),
> sum += vu[0];
> ::auNonOptimize = sum;
> return (int64_t)duration_cast<nanoseconds>(
> high_resolution_clock::now() - start ).count() / 1.0e9;
> };
> auto cSort = []( unsigned *begin, unsigned *end )
> {
> qsort( begin, end - begin, sizeof(unsigned),
> []( void const *left, void const *right ) -> int
> {
> unsigned
> &l = *(unsigned *)left,
> &r = *(unsigned *)right;
> return l < r ? -1 : l > r ? 1 : 0;
> } );
> };
> auto cppSort = []( unsigned *begin, unsigned *end )
> {
> sort( begin, end );
> };
> double tC, tCpp, rel;
> for( size_t s = 2, rounds; s <= V_MAX; s *= 2 )
> rounds = (V_MAX_ROUNDS / s) * V_MAX_ROUNDS,
> tC = timedSort( cSort, s, rounds ),
> tCpp = timedSort( cppSort, s, rounds ),
> rel = tC / tCpp * 100.0,
> cout << s << ": " << (int)(rel + 0.5) << "%" << endl;
> }
>
> These are the results on my computer (Ryzen Threadripper 3990X):
>
> 2: 279%
> 4: 303%
> 8: 531%
> 16: 611%
> 32: 543%
> 64: 627%
> 128: 536%
> 256: 481%
> 512: 402%
> 1024: 316%
> 2048: 260%
> 4096: 230%
>
> Up to 6,3 times faster with less coding !

If a C programmer has concerns about qsort performance he just
make it inline removing the function comparison pointer and using
the function directly.

Re: qsort() vs. std::sort

<t4bre0$6ni$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21510&group=comp.lang.c#21510

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: qsort() vs. std::sort
Date: Wed, 27 Apr 2022 18:36:38 +0200
Organization: A noiseless patient Spider
Lines: 92
Message-ID: <t4bre0$6ni$2@dont-email.me>
References: <t4092b$des$1@dont-email.me>
<20115f32-0400-4813-8ddb-5be877eba3d6n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 27 Apr 2022 16:36:16 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ffa14d285614a002c5311770cc55b676";
logging-data="6898"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18RqMrM+GDatA3dYYAgKjji17c6Qa32E+M="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:uy0xzbhEbclRbZxf7T9gNVfHddo=
In-Reply-To: <20115f32-0400-4813-8ddb-5be877eba3d6n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Wed, 27 Apr 2022 16:36 UTC

Am 27.04.2022 um 13:21 schrieb Thiago Adams:
> On Saturday, April 23, 2022 at 4:15:38 AM UTC-3, Bonita Montero wrote:
>> I just measured how superior C++'s std::sort is over C's qsort:
>>
>> #include <iostream>
>> #include <vector>
>> #include <random>
>> #include <chrono>
>> #include <cstdlib>
>> #include <algorithm>
>> #include <memory>
>> #include <atomic>
>>
>> using namespace std;
>> using namespace chrono;
>>
>> atomic_uint auNonOptimize;
>>
>> int main()
>> {
>> constexpr size_t
>> V_MAX = 0x1000,
>> V_MAX_ROUNDS = 0x4000;
>> vector<unsigned>
>> vu( V_MAX, 0 ),
>> vuSort( V_MAX, 0 );
>> mt19937_64 mt;
>> uniform_int_distribution<unsigned> uid( 0, -1 );
>> for( unsigned &u : vu )
>> u = uid( mt );
>> auto timedSort = [&]<typename SortFn>( SortFn sortFn, size_t n, size_t
>> rounds ) -> double
>> requires requires( SortFn sortFn, unsigned *p ) { { sortFn( p, p ) }; }
>> {
>> auto start = high_resolution_clock::now();
>> unsigned sum = 0;
>> while( rounds-- )
>> copy( vu.begin(), vu.begin() + n, vuSort.begin() ),
>> sortFn( to_address( vuSort.begin() ), to_address( vuSort.begin() ) + n ),
>> sum += vu[0];
>> ::auNonOptimize = sum;
>> return (int64_t)duration_cast<nanoseconds>(
>> high_resolution_clock::now() - start ).count() / 1.0e9;
>> };
>> auto cSort = []( unsigned *begin, unsigned *end )
>> {
>> qsort( begin, end - begin, sizeof(unsigned),
>> []( void const *left, void const *right ) -> int
>> {
>> unsigned
>> &l = *(unsigned *)left,
>> &r = *(unsigned *)right;
>> return l < r ? -1 : l > r ? 1 : 0;
>> } );
>> };
>> auto cppSort = []( unsigned *begin, unsigned *end )
>> {
>> sort( begin, end );
>> };
>> double tC, tCpp, rel;
>> for( size_t s = 2, rounds; s <= V_MAX; s *= 2 )
>> rounds = (V_MAX_ROUNDS / s) * V_MAX_ROUNDS,
>> tC = timedSort( cSort, s, rounds ),
>> tCpp = timedSort( cppSort, s, rounds ),
>> rel = tC / tCpp * 100.0,
>> cout << s << ": " << (int)(rel + 0.5) << "%" << endl;
>> }
>>
>> These are the results on my computer (Ryzen Threadripper 3990X):
>>
>> 2: 279%
>> 4: 303%
>> 8: 531%
>> 16: 611%
>> 32: 543%
>> 64: 627%
>> 128: 536%
>> 256: 481%
>> 512: 402%
>> 1024: 316%
>> 2048: 260%
>> 4096: 230%
>>
>> Up to 6,3 times faster with less coding !
>
> If a C programmer has concerns about qsort performance he just
> make it inline removing the function comparison pointer and using
> the function directly.

1. You can't inline a function with arbitrary recursion.
2. The comparison-function for qsort() is never inlined.

Re: qsort() vs. std::sort

<082pji-tuv1.ln1@wilbur.25thandClement.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21521&group=comp.lang.c#21521

  copy link   Newsgroups: comp.lang.c++ comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 27 Apr 2022 19:15:02 -0500
Message-ID: <082pji-tuv1.ln1@wilbur.25thandClement.com>
From: will...@25thandClement.com (William Ahern)
Subject: Re: qsort() vs. std::sort
Newsgroups: comp.lang.c++,comp.lang.c
References: <t4092b$des$1@dont-email.me>
User-Agent: tin/2.4.4-20191224 ("Millburn") (OpenBSD/7.0 (amd64))
Date: Wed, 27 Apr 2022 17:03:44 -0700
Lines: 75
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-h2Ty51EC76Y31MiroezAEKntUrqgaCwL44of7OxIt/vvJzFD9773uiAXO4OTwIyRfcCRDteZF36k7Ji!eTPnkAb4uX7Kng2N3jgr+KXNbfDQXJJ3M4suEMNN2YW5AjJnfVduQmJZMtvStpCanrw=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 3731
 by: William Ahern - Thu, 28 Apr 2022 00:03 UTC

Bonita Montero <Bonita.Montero@gmail.com> wrote:
> I just measured how superior C++'s std::sort is over C's qsort:
<snip>
> These are the results on my computer (Ryzen Threadripper 3990X):
>
> 2: 279%
> 4: 303%
> 8: 531%
> 16: 611%
> 32: 543%
> 64: 627%
> 128: 536%
> 256: 481%
> 512: 402%
> 1024: 316%
> 2048: 260%
> 4096: 230%
>
> Up to 6,3 times faster with less coding !

Unsurprising that inlining can improve performance. Your raw numbers are
useless, though, as you're comparing different algorithms (C++ header
template vs libc), which shall be demonstrated below. Also, technically
speaking your gripe is largely with toolchains (particularly wrt the
performance of standard library interfaces like qsort) as there's nothing
preventing toolchains from inlining qsort and the provided callback; for
historical reasons they simply don't. Newer languages and their toolchains,
like Rust, statically compile everything, including the standard library,
which is something C toolchain and runtime environment providers could just
as well do also. Rust Vector's sort API implicitly relies on static
compilation, otherwise it would suffer the same runtime indirect call issue
as in typical C environments.

I'm unable to compile your C++ code on OpenBSD 7.0; it appears to use
features that are too new. I ran the following LTO inlining experiment on
Alpine Linux 3.15 but using the qsort implementation from OpenBSD 7.0, which
was easiest to compile outside its source tree with minimal modifications
(changing qsort and heapsort to openbsd_qsort and openbsd_heapsort,
respectively, and adding a forward declaration for openbsd_heapsort). As you
can see, OpenBSD's qsort is implemented using heapsort. Not sure which
algorithm Alpine Linux's C++ std::sort is using, but it's either a different
algorithm entirely or a significantly different implementation.

alpine-3-15:/tmp$ make qsort-inline qsort-extern
cc -flto -O2 -march=native -Wall -c -o openbsd-qsort.o openbsd-qsort.c
cc -flto -O2 -march=native -Wall -c -o openbsd-heapsort.o openbsd-heapsort.c
g++ -o qsort-inline qsort.cc openbsd-qsort.o openbsd-heapsort.o -flto -O2 -march=native -Wall -std=c++2a -fvisibility=hidden
cc -shared -o libopenbsd-qsort.so openbsd-qsort.o openbsd-heapsort.o -flto -O2 -march=native -Wall
g++ -o qsort-extern qsort.cc -flto -O2 -march=native -Wall -std=c++2a -fvisibility=hidden -L. -Wl,-rpath="." -lopenbsd-qsort
alpine-3-15:/tmp$ ./qsort-inline
2: 54%
4: 59%
8: 39%
16: 75%
32: 86%
64: 131%
128: 160%
256: 186%
512: 186%
1024: 180%
2048: 151%
4096: 144%
alpine-3-15:/tmp$ ./qsort-extern
2: 75%
4: 95%
8: 84%
16: 176%
32: 195%
64: 285%
128: 362%
256: 490%
512: 588%
1024: 302%
2048: 246%
4096: 230%

Re: qsort() vs. std::sort

<kk2pji-tuv1.ln1@wilbur.25thandClement.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21522&group=comp.lang.c#21522

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!buffer1.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 27 Apr 2022 19:15:03 -0500
Message-ID: <kk2pji-tuv1.ln1@wilbur.25thandClement.com>
From: will...@25thandClement.com (William Ahern)
Subject: Re: qsort() vs. std::sort
Newsgroups: comp.lang.c
References: <t4092b$des$1@dont-email.me> <20115f32-0400-4813-8ddb-5be877eba3d6n@googlegroups.com> <t4bre0$6ni$2@dont-email.me>
User-Agent: tin/2.4.4-20191224 ("Millburn") (OpenBSD/7.0 (amd64))
Date: Wed, 27 Apr 2022 17:10:28 -0700
Lines: 17
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-FNclY2UjBWYEHfHOrLv8Q+Zse979pT0U1XgFz38+h2w4bH/ABGezZI7HPH1Kwk7E2MK2mQrUvN0ZpLu!580KPnHJ5eCn9vLbS/J3sQ6npt0tzPNVAh6HHFL2wXqn6PFKETnZLz5R6DxoVGcxghQ=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 1742
 by: William Ahern - Thu, 28 Apr 2022 00:10 UTC

Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Am 27.04.2022 um 13:21 schrieb Thiago Adams:
<snip>
>> If a C programmer has concerns about qsort performance he just
>> make it inline removing the function comparison pointer and using
>> the function directly.
>
> 1. You can't inline a function with arbitrary recursion.

The same is true in C++.

> 2. The comparison-function for qsort() is never inlined.

That's a toolchain issue. Nothing prevents C environments from inlining
qsort and it's callback; most simply haven't done so historically. Other
language environments like Rust implicitly rely on static compilation to
avoid the cost of an indirect function call on every comparison.

Re: qsort() vs. std::sort

<t4d6sn$ol3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21523&group=comp.lang.c#21523

  copy link   Newsgroups: comp.lang.c++ comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++,comp.lang.c
Subject: Re: qsort() vs. std::sort
Date: Thu, 28 Apr 2022 06:58:22 +0200
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <t4d6sn$ol3$1@dont-email.me>
References: <t4092b$des$1@dont-email.me>
<082pji-tuv1.ln1@wilbur.25thandClement.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 28 Apr 2022 04:57:59 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="dc94a027e7a59a79fd3b6f06a8ee6283";
logging-data="25251"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yVjvw77FF90J1tKOia+7e0BcRWoKbLYc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:I2e2q5H1/zayPSYRYe6O8xT7PUc=
In-Reply-To: <082pji-tuv1.ln1@wilbur.25thandClement.com>
Content-Language: de-DE
 by: Bonita Montero - Thu, 28 Apr 2022 04:58 UTC

> Unsurprising that inlining can improve performance. Your raw numbers are
> useless, though, as you're comparing different algorithms (C++ header
> template vs libc), ...

No, the numbers aren't useless, they compare the C- and the C++-way
to sort things.

> Also, technically
> speaking your gripe is largely with toolchains (particularly wrt the
> performance of standard library interfaces like qsort) as there's nothing
> preventing toolchains from inlining qsort ...

LOL, there's no C compiler which will do that. The qsort-function is
just a static piece of code and nothing is inlined.

Re: qsort() vs. std::sort

<t4d6ut$om6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21524&group=comp.lang.c#21524

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c
Subject: Re: qsort() vs. std::sort
Date: Thu, 28 Apr 2022 06:59:33 +0200
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <t4d6ut$om6$1@dont-email.me>
References: <t4092b$des$1@dont-email.me>
<20115f32-0400-4813-8ddb-5be877eba3d6n@googlegroups.com>
<t4bre0$6ni$2@dont-email.me> <kk2pji-tuv1.ln1@wilbur.25thandClement.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 28 Apr 2022 04:59:09 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="dc94a027e7a59a79fd3b6f06a8ee6283";
logging-data="25286"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19SFslHRykBTlKQ/8Pbli83BjxVfHnv8fw="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:V84uuzHuXnOhg1jxBgC+RDdemfw=
In-Reply-To: <kk2pji-tuv1.ln1@wilbur.25thandClement.com>
Content-Language: de-DE
 by: Bonita Montero - Thu, 28 Apr 2022 04:59 UTC

Am 28.04.2022 um 02:10 schrieb William Ahern:
> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>> Am 27.04.2022 um 13:21 schrieb Thiago Adams:
> <snip>
>>> If a C programmer has concerns about qsort performance he just
>>> make it inline removing the function comparison pointer and using
>>> the function directly.
>>
>> 1. You can't inline a function with arbitrary recursion.
>
> The same is true in C++.
>
>> 2. The comparison-function for qsort() is never inlined.
>
> That's a toolchain issue. Nothing prevents C environments from inlining
> qsort and it's callback; ...

The qsort-function is a static piece of code and the comparison
-function is never inlined.

Re: qsort() vs. std::sort

<t4dcge$1fal$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21525&group=comp.lang.c#21525

  copy link   Newsgroups: comp.lang.c++ comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!NK0c7qMEn6mmBWqphs27pg.user.46.165.242.75.POSTED!not-for-mail
From: nos...@thanks.invalid (Juha Nieminen)
Newsgroups: comp.lang.c++,comp.lang.c
Subject: Re: qsort() vs. std::sort
Date: Thu, 28 Apr 2022 06:33:52 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <t4dcge$1fal$1@gioia.aioe.org>
References: <t4092b$des$1@dont-email.me> <082pji-tuv1.ln1@wilbur.25thandClement.com>
Injection-Info: gioia.aioe.org; logging-data="48469"; posting-host="NK0c7qMEn6mmBWqphs27pg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: tin/2.4.3-20181224 ("Glen Mhor") (UNIX) (Linux/5.10.103-grsec-kapsi (x86_64))
X-Notice: Filtered by postfilter v. 0.9.2
 by: Juha Nieminen - Thu, 28 Apr 2022 06:33 UTC

In comp.lang.c++ William Ahern <william@25thandclement.com> wrote:
> Unsurprising that inlining can improve performance.

The performance boost doesn't come from inlining. One single function call
wouldn't make a difference when it comes to sorting. It doesn't matter if
the std::sort() function gets inlined into the calling code or whether
the compiler decides to make it a separate function. That would only be
a difference of a few clock cycles at most.

The difference comes from what could perhaps be called "reverse inlining"
(which happens thanks to std::sort() being a template). In other words,
rather than std::sort() being embedded into the calling code, data from
the calling code gets embedded into the std::sort() implementation.
(In other words, we are doing the "inlining" kind of in reverse.)

This means that when std::sort() needs to, for example, compare two
elements, it knows exactly the type of the element and can do the
comparison "inlined" in its own code, rather than having to call some
external function provided by the caller.

> Your raw numbers are
> useless, though, as you're comparing different algorithms (C++ header
> template vs libc)

That's not a difference in algorithm. That's a difference in implementation.
For all we know std::sort() and qsort() could both use the exact same
sorting algorithm.

Note that making std::sort() a template isn't done primarily because it
makes it more efficient (it's a really nice bonus, but it's not the primary
reason why it's done). The main reason why it's a template is because
swapping elements in C++ may not always be possible by merely swapping
the underlying bytes of those elements. In other words, the swapping
is type-dependent. Achieving this is easiest with a template.

Re: qsort() vs. std::sort

<et4qji-e2c2.ln1@wilbur.25thandClement.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21529&group=comp.lang.c#21529

  copy link   Newsgroups: comp.lang.c++ comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!buffer1.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 28 Apr 2022 05:00:02 -0500
Message-ID: <et4qji-e2c2.ln1@wilbur.25thandClement.com>
From: will...@25thandClement.com (William Ahern)
Subject: Re: qsort() vs. std::sort
Newsgroups: comp.lang.c++,comp.lang.c
References: <t4092b$des$1@dont-email.me> <082pji-tuv1.ln1@wilbur.25thandClement.com> <t4dcge$1fal$1@gioia.aioe.org>
User-Agent: tin/2.4.4-20191224 ("Millburn") (OpenBSD/7.0 (amd64))
Date: Thu, 28 Apr 2022 02:55:26 -0700
Lines: 90
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-4NgNRmpV1xDGk8UCWizJVuCOmIQxZVt46BIQcuGO5ckVmNfHk2jS2cl5baoV3VFhrB0OOXH33cyGctb!3m1+8xriHywLIDEzsanPRSEcugXY6UMotyEcuzrRLqAHdNfnuCHtSAh933PqhQ6hNSM=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 5741
 by: William Ahern - Thu, 28 Apr 2022 09:55 UTC

Juha Nieminen <nospam@thanks.invalid> wrote:
> In comp.lang.c++ William Ahern <william@25thandclement.com> wrote:
>> Unsurprising that inlining can improve performance.
>
> The performance boost doesn't come from inlining. One single function call
> wouldn't make a difference when it comes to sorting. It doesn't matter if
> the std::sort() function gets inlined into the calling code or whether
> the compiler decides to make it a separate function. That would only be
> a difference of a few clock cycles at most.

I posted two sets of benchmarks, one for qsort-inline and another for
qsort-extern. qsort-inline clearly has a substantially different performance
profile, and that could only happen if the comparison callback itself was
inlined into the sort routine. See below for the disassembly.

> The difference comes from what could perhaps be called "reverse inlining"
> (which happens thanks to std::sort() being a template). In other words,
> rather than std::sort() being embedded into the calling code, data from
> the calling code gets embedded into the std::sort() implementation.
> (In other words, we are doing the "inlining" kind of in reverse.)
>
> This means that when std::sort() needs to, for example, compare two
> elements, it knows exactly the type of the element and can do the
> comparison "inlined" in its own code, rather than having to call some
> external function provided by the caller.

This is exactly what is happening in the qsort-inline trial I posted: the
comparison callback function is inlined into the qsort implementation. The
reason I couldn't just use -flto with the C runtime qsort is that -flto in
clang and GCC require the intermediate code for both the comparison callback
*and* the qsort implementation itself, and that information isn't included
in Alpine's C runtime--neither the dynamic libc.so nor static libc.a. In
theory the intermediate code *could* be included, just like a system C
runtime can ship with debug symbols.

Below is the dissassembly of some relevant portions of qsort-inline proving
inlining. After recompiling qsort-inline with debug symbols (-g), the
fragment was generated using `objdump -drwC -S -l`, which interleaves
assembly blocks with the originating source block.

This first fragment begins inside qsort (at openbsd-qsort.c line 147, inside
the inner introsort routine) and ends with code from the comparator lambda
in the post C++ benchmark code (at qsort.cc line 51, +/- a couple lines from
the original posted code), without any interveaning calls (only jumps),
showing that the comparator function is inlined into the sorting
implementation itself:

/tmp/comp.lang.c-qsort/openbsd-qsort.c:147
if (n > 7) {
1e40: 48 83 fb 07 cmp $0x7,%rbx
1e44: 74 2c je 1e72 <introsort.constprop.0+0x72>
introsort.constprop.0():
/tmp/comp.lang.c-qsort/qsort.cc:51
return l < r ? -1 : l > r ? 1 : 0;
1e46: 8b 08 mov (%rax),%ecx
1e48: 8b 32 mov (%rdx),%esi

This second fragment begins inside the timedSort lambda, showing that
qsort itself is inlined into the lambda:

operator()<main()::<lambda(unsigned int*, unsigned int*)> >():
/tmp/comp.lang.c-qsort/qsort.cc:35
while( rounds-- )
1315: 4c 8d 78 ff lea -0x1(%rax),%r15
1319: 4c 89 fd mov %r15,%rbp
131c: 0f 1f 40 00 nopl 0x0(%rax)
unsigned int* std::__copy_move<false, true, std::random_access_iterator_tag>::__copy_m<unsigned int>(unsigned int const*, unsigned int const*, unsigned int*):
/usr/include/c++/10.3.1/bits/stl_algobase.h:426
__builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
1320: 48 8b 7c 24 30 mov 0x30(%rsp),%rdi
1325: 48 89 da mov %rbx,%rdx
1328: e8 03 fd ff ff call 1030 <memmove@plt>
__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int, std::allocator<unsigned int> > >::__normal_iterator(unsigned int* const&):
/usr/include/c++/10.3.1/bits/stl_iterator.h:979
: _M_current(__i) { }
132d: 48 8b 7c 24 30 mov 0x30(%rsp),%rdi
openbsd_qsort():
/tmp/comp.lang.c-qsort/openbsd-qsort.c:225
{
size_t i, maxdepth = 0;
int swaptype;

/* Approximate 2*ceil(lg(n + 1)) */
for (i = n; i > 0; i >>= 1)
1332: 4c 89 e8 mov %r13,%rax

The OpenBSD source code used can be found at

https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdlib/qsort.c?annotate=1.18
https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdlib/heapsort.c?annotate=1.11

Re: qsort() vs. std::sort

<lk6qji-no21.ln1@wilbur.25thandClement.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21531&group=comp.lang.c#21531

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 28 Apr 2022 05:30:02 -0500
Message-ID: <lk6qji-no21.ln1@wilbur.25thandClement.com>
From: will...@25thandClement.com (William Ahern)
Subject: Re: qsort() vs. std::sort
Newsgroups: comp.lang.c
References: <t4092b$des$1@dont-email.me> <20115f32-0400-4813-8ddb-5be877eba3d6n@googlegroups.com> <t4bre0$6ni$2@dont-email.me> <kk2pji-tuv1.ln1@wilbur.25thandClement.com> <t4d6ut$om6$1@dont-email.me>
User-Agent: tin/2.4.4-20191224 ("Millburn") (OpenBSD/7.0 (amd64))
Date: Thu, 28 Apr 2022 03:24:53 -0700
Lines: 43
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-3Wkhoc28VdMzb4AN0sfHejUHeXQf3kmTyNTkgpAOJ1XAAR9AcDWer2wtkus+5awFCZ1in6suBuTuBtX!tfgOFQKgNg0FsXGHxD5hB229Q+03DpGKPX+bpOCerDFvFjB3kjYQrTlLqgiEnh9JPUc=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 3195
 by: William Ahern - Thu, 28 Apr 2022 10:24 UTC

Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Am 28.04.2022 um 02:10 schrieb William Ahern:
>> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>> Am 27.04.2022 um 13:21 schrieb Thiago Adams:
>> <snip>
>>>> If a C programmer has concerns about qsort performance he just
>>>> make it inline removing the function comparison pointer and using
>>>> the function directly.
>>>
>>> 1. You can't inline a function with arbitrary recursion.
>>
>> The same is true in C++.
>>
>>> 2. The comparison-function for qsort() is never inlined.
>>
>> That's a toolchain issue. Nothing prevents C environments from inlining
>> qsort and it's callback; ...
>
> The qsort-function is a static piece of code and the comparison
> -function is never inlined.

See disassembly posted elsethread. I've given all the information needed to
quickly reproduce this for yourself, assuming some minimal familiarity with
GCC.

Alpine Linux isn't even needed, it's just what I started with out of
caution. I just now reproduced the distinctive benchmark profiles between
qsort-inline and qsort-extern on macOS 12.3.1 after removing -Wl,-rpath from
CFLAGS. (The macOS/mach-O linker doesn't recognize the -rpath option, but
it's linker embeds and uses the original location by default, anyhow.) I
don't know off-hand how to get a proper disassembly on macOS (-flto
complicates things considerably), but there's no reason to believe that the
comparator wasn't inlined like it was on Linux.

As another poster mentioned, templates are good for much more than
performance inlining. But I've always had a gripe with the qsort vs
std::qsort meme in particular: it confuses C toolchains and historical
linking semantics with the language itself. It should be clear, now, that C
language semantics don't prevent inlining of a comparator passed to a
sorting function; now even one defined externally to the invoking source
unit. (Unless somehow I've missed that a standard routine can't be inlined
without violating the standard--e.g. that the symbol has to have a unique
address.)

Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor