🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1576

Re: Inversion-of-control and dependency-injection in C

#1576
From: fir
Date: Mon, 16 Nov 2026 17:00
156 lines
6219 bytes
Bonita Montero pisze:
> I wrote a comparison between std::sort() and qsort() to sort an array of
> 4096 ints (fits in L1). On my Zen4-PC std::sort() is 4.8 times faster.
> Any question ?
> 
> #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;
> }
> 
> 
so maybe try it right c way




typedef unsigned int UI32;

UI32 * RadixSortUnsigned(UI32 * pData, UI32 * pTemp, size_t count)
{
size_t mIndex[4][256] = {0};            // index matrix
UI32 *pDst, *pSrc, *pTmp;
size_t i,j,m,n;
UI32 u;

     for(i = 0; i < count; i++){         // generate histograms
         u = pData[i];
         for(j = 0; j < 4; j++){
             mIndex[j][(size_t)(u & 0xff)]++;
             u >>= 8;
         }
     }
     for(j = 0; j < 4; j++){             // convert to indices
         n = 0;
         for(i = 0; i < 256; i++){
             m = mIndex[j][i];
             mIndex[j][i] = n;
             n += m;
         }
     }
     pDst = pTemp;                       // radix sort
     pSrc = pData;
     for(j = 0; j < 4; j++){
         for(i = 0; i < count; i++){
             u = pSrc[i];
             m = (size_t)(u >> (j<<3)) & 0xff;
             pDst[mIndex[j][m]++] = u;
         }
         pTmp = pSrc;
         pSrc = pDst;
         pDst = pTmp;
     }
     return(pSrc);
}


typedef          int  I32;
typedef unsigned int UI32;

I32 * RadixSortSigned(I32 * pData, I32 * pTemp, size_t count)
{
size_t mIndex[4][256] = {0};            // index matrix
UI32 *pDst, *pSrc, *pTmp;
size_t i,j,m,n;
UI32 u;

     for(i = 0; i < count; i++){         // generate histograms
         u = pData[i];
         for(j = 0; j < 4; j++){
             if(j != 3)                  //  signed integer handling
                 mIndex[j][(size_t)(u & 0xff)]++;
             else
                 mIndex[j][(size_t)((u^0x80) & 0xff)]++;
             u >>= 8;
         }
     }
     for(j = 0; j < 4; j++){             // convert to indices
         n = 0;
         for(i = 0; i < 256; i++){
             m = mIndex[j][i];
             mIndex[j][i] = n;
             n += m;
         }
     }
     pDst = (UI32 *)pTemp;               // radix sort
     pSrc = (UI32 *)pData;
     for(j = 0; j < 4; j++){
         for(i = 0; i < count; i++){
             u = pSrc[i];
             if(j != 3)                  //  signed integer handling
                 m = (size_t)(u >> (j<<3)) & 0xff;
             else
                 m = (size_t)((u >> (j<<3))^0x80) & 0xff;
             pDst[mIndex[j][m]++] = u;
         }
         pTmp = pSrc;
         pSrc = pDst;
         pDst = pTmp;
     }
     return((I32 *)pSrc);
}

Technical Details
Message-ID: <11dfcqi$3ntok$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com> <11d54jb$80v7$1@raubtier-asyl.eternal-september.org> <11d56pj$8sfe$1@dont-email.me> <11df75b$3lnf4$1@raubtier-asyl.eternal-september.org> <11df7h3$3lpoh$1@dont-email.me> <11dfb43$3n757$1@raubtier-asyl.eternal-september.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: fir <profesor.fir@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: Inversion-of-control and dependency-injection in C
Date: Mon, 16 Nov 2026 18:00:01 +0100
Organization: A noiseless patient Spider
Lines: 156
Message-ID: <11dfcqi$3ntok$1@dont-email.me>
References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com>
 <11d54jb$80v7$1@raubtier-asyl.eternal-september.org>
 <11d56pj$8sfe$1@dont-email.me>
 <11df75b$3lnf4$1@raubtier-asyl.eternal-september.org>
 <11df7h3$3lpoh$1@dont-email.me>
 <11dfb43$3n757$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: Mon, 16 Nov 2026 17:00:03 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3929876"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/Wm5KkjScvIULPzbSKH6x3/zUxe45KmSc="; posting-host="ffaeb1a4357f66637402e7443ff79a2b"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101
 Firefox/140.0 SeaMonkey/2.53.24
Cancel-Lock: sha1:/AUYiVKWmvSEa+DVVVZ60WnFemk= sha256:coW78mQYr73+eqORcBCH4y5fEGbwHMjxkoVTGc33OY0=
	sha1:0k2QMONAqfFhWbTbgdQlB+oQ61c= sha256:XSKMzfk9y/9bn5qBNxa6Jw0N59XxRGM45QT+th3LFkY=
In-Reply-To: <11dfb43$3n757$1@raubtier-asyl.eternal-september.org>