Article View: comp.lang.c
Article #1926Re: C and its peasant 'ancestors'
From: bartc
Date: Sat, 28 Nov 2026 15:28
Date: Sat, 28 Nov 2026 15:28
204 lines
7471 bytes
7471 bytes
On 28/11/2026 04:54, Bonita Montero wrote:
> Am 27.11.2026 um 22:36 schrieb Lawrence D’Oliveiro:
>
>> ... the end result is a horrible and unmaintainable mess.
> The abstractions of C++ make C++ more readable and maintaineable as C.
>
Yet every example of C++ you've ever posted is always an unreadable,
ugly mess of syntax.
Not all that is the fault of the language - you seem to delight in using
every obscure feature possible and have nested functions everywhere.
Below is a recent post of yours about sorting in C++. It took me a while
to realise it was actually testing two kinds of sort!
It is frankly diabolical. It seems designed to obfuscate as much as
possible.
Below yours, is a version in my language, just to show the contrast.
This one only tests a qsort approach, but everything about it is
straightforward.
And below that, is a C port. Not quite as clean, but considerably less
scary-looking than your C++.
As for timings, then for 10000 sorts of 4096 random integers in the i32
range, they were:
C++/g++-O3 qsrt 3.5 seconds
C++/g++-O3 stdsort 1.5 seconds
My lang 2.5 seconds
C/gcc-O2 2.5 seconds
C/bcc 2.5 seconds
But I don't know what's going on in your qsrt, and I don't know what
stdsort does; these will be algorithmic differences.
However, this is what happens when optimisation is turned off in all
cases (in my tools, that just means locals stay in memory):
C++/g++-O0 qsrt 7.0 seconds
C++/g++-O0 stdsort 16.8 seconds
My lang -no 3.7 seconds
C/gcc-O0 4.0 seconds
C/bcc -no 4.0 seconds
Now C++'s stdsort is more than twice as slow as your qsrt! And 4 times
as slow as that 'isort' in C.
There is a more than 10:1 difference between optimised/unoptimised for
this C++ standard sort. Here is one more timing:
QQ 15.0 seconds
This is my dynamic scripting language on the same task. The sort routine
(same as isort) is implemented /in the language/, it is not called as an
internal function written in native code.
So unoptimised C++ is slower than a scripting language!
-----------------------------------------
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <chrono>
#include <vector>
#include <random>
using namespace std;
using namespace chrono;
int main()
{
constexpr size_t
N = 0x1000,
Rounds = 10'000;
auto test = []( auto fn ) -> double
{
time_point start = steady_clock::now();
for( size_t r = Rounds; r; --r )
fn();
duration dur = steady_clock::now() - start;
return (double)duration_cast<nanoseconds>( dur ).count();
};
vector<int>
origin( N ),
srt( N );
mt19937_64 mt;
for( int &i : origin )
i = (int)mt();
int dummy = 0;
auto cpy = [&] { copy( origin.begin(), origin.end(), srt.begin() );
dummy += srt[0]; };
double tRef = test( cpy );
auto qsrt = [&]
{
cpy();
qsort( srt.data(), srt.size(), sizeof( int ), +[]( const
void *lhs, const void *rhs ) -> int
{
int &iLhs = *(int *)lhs, &iRhs = *(int *)rhs;
if( iLhs == iRhs )
return 0;
return iLhs < iRhs ? -1 : 1;
} );
dummy += srt[0];
};
double tQsrt = test( qsrt ) - tRef;
auto stdSrt = [&]
{
cpy();
sort( srt.begin(), srt.end(), less() );
dummy += srt[0];
};
double tStdSrt = test( stdSrt ) - tRef;
cout << tQsrt / tStdSrt << endl;
return dummy;
}
-----------------------------------------
proc main =
const nn = 4096
[nn]int data, sdata # int is i64
for i to nn do
data[i] := mrandomrange(i32.min, i32.max)
end
to 10'000 do
sdata := data
isort(sdata, 1, sdata.len)
end
for i to nn when i not in 10..nn-10 do
println i, sdata[i]
end
end
proc isort([]int &data, int ll, rr)=
int pivot, i := ll, j := rr
pivot:=data[(ll+rr)/2]
repeat
while pivot > data[i] and i<rr do ++i end
while pivot < data[j] and j>ll do --j end
if i<=j then
swap(data[i], data[j])
++i
--j
end
until i>j
if ll<j then isort(data, ll, j) end
if i<rr then isort(data, i, rr) end
end
--------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void isort(int* data, int ll, int rr) {
int pivot, i = ll, j = rr, t;
pivot = data[(ll+rr)/2];
do {
while (pivot > data[i] && i<rr) ++i;
while (pivot < data[j] && j>ll) --j;
if (i<=j) {
t=data[i]; data[i]=data[j]; data[j]=t;
++i;
--j;
}
} while (!(i>j));
if (ll<j) isort(data, ll, j);
if (i<rr) isort(data, i, rr);
}
int main(void) {
enum {N=4096};
int data[N], sdata[N], i;
for (i=0; i<N; ++i) data[i] = (rand()<<15) | rand();
for (i=0; i<10000; ++i) {
memcpy(sdata, data, sizeof(data));
isort(sdata, 0, N-1);
}
for (i=0; i<N; ++i) if (i<10 || i>(N-10)) printf("%4d %d\n", i,
sdata[i]);
}
Technical Details
Message-ID:
<11eerur$2me3k$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11dup6q$v4j9$1@dont-email.me> <11e0kde$1jpvu$1@dont-email.me> <11e0r13$1lqm6$1@dont-email.me> <11e0uc0$1mtpv$1@raubtier-asyl.eternal-september.org> <11e7fj4$37l9$1@dont-email.me> <11e8oro$ft4l$1@raubtier-asyl.eternal-september.org> <11e8stp$hajr$1@dont-email.me> <11e8ti0$hhqa$1@dont-email.me> <11e8v24$i2mc$2@raubtier-asyl.eternal-september.org> <11e9045$ihk3$1@dont-email.me> <11e90a8$ikh0$1@dont-email.me> <11e90mv$io87$2@raubtier-asyl.eternal-september.org> <11e91ns$j5dv$1@dont-email.me> <11e92ff$jclo$1@raubtier-asyl.eternal-september.org> <11e95j2$kimu$1@dont-email.me> <11e96aa$kpta$1@raubtier-asyl.eternal-september.org> <11ect54$20qpr$3@dont-email.me> <11edmpn$2938p$1@raubtier-asyl.eternal-september.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: bartc <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: C and its peasant 'ancestors' Date: Sat, 28 Nov 2026 15:28:27 +0000 Organization: A noiseless patient Spider Lines: 204 Message-ID: <11eerur$2me3k$1@dont-email.me> References: <11dup6q$v4j9$1@dont-email.me> <11e0kde$1jpvu$1@dont-email.me> <11e0r13$1lqm6$1@dont-email.me> <11e0uc0$1mtpv$1@raubtier-asyl.eternal-september.org> <11e7fj4$37l9$1@dont-email.me> <11e8oro$ft4l$1@raubtier-asyl.eternal-september.org> <11e8stp$hajr$1@dont-email.me> <11e8ti0$hhqa$1@dont-email.me> <11e8v24$i2mc$2@raubtier-asyl.eternal-september.org> <11e9045$ihk3$1@dont-email.me> <11e90a8$ikh0$1@dont-email.me> <11e90mv$io87$2@raubtier-asyl.eternal-september.org> <11e91ns$j5dv$1@dont-email.me> <11e92ff$jclo$1@raubtier-asyl.eternal-september.org> <11e95j2$kimu$1@dont-email.me> <11e96aa$kpta$1@raubtier-asyl.eternal-september.org> <11ect54$20qpr$3@dont-email.me> <11edmpn$2938p$1@raubtier-asyl.eternal-september.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 28 Nov 2026 15:28:28 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2832500"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+o96C5V7a0JzeoOlN8Wdfu"; posting-host="0d82bebd73fa202dc7ad3cf5c365f7ca" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:NJFEl7t8eBAeI80urlp4Qf5d4Es= sha256:xE4JfpAnCanwaLZzcpaCIfHWIGcFuwaqtICLUbBcHpM= sha1:oeWAYFv0G7EOTUezmAXYi9fRy6c= sha256:oz1Q+wdDo1f0VClnLmTBpfhVoheye3tBcjsm9W+A0bo= Content-Language: en-GB In-Reply-To: <11edmpn$2938p$1@raubtier-asyl.eternal-september.org>