Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #83681

qsort() vs. std::sort

From Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups comp.lang.c++, comp.lang.c
Subject qsort() vs. std::sort
Date 2022-04-23 09:15 +0200
Organization A noiseless patient Spider
Message-ID <t4092b$des$1@dont-email.me> (permalink)

Cross-posted to 2 groups.

Show all headers | View raw


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 !

Back to comp.lang.c++ | Previous | NextNext in thread | Find similar | Unroll thread


Thread

qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-23 09:15 +0200
  Re: qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-23 09:31 +0200
  Re: qsort() vs. std::sort Juha Nieminen <nospam@thanks.invalid> - 2022-04-25 05:56 +0000
    Re: qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-25 08:27 +0200
  Re: qsort() vs. std::sort Öö Tiib <ootiib@hot.ee> - 2022-04-25 01:16 -0700
    Re: qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-25 12:59 +0200
      Re: qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-25 13:32 +0200
        Re: qsort() vs. std::sort Öö Tiib <ootiib@hot.ee> - 2022-04-25 05:26 -0700
          Re: qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-25 17:41 +0200
            Re: qsort() vs. std::sort Öö Tiib <ootiib@hot.ee> - 2022-04-26 00:46 -0700
              Re: qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-26 12:19 +0200
      Re: qsort() vs. std::sort Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-04-25 05:24 -0700
        Re: qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-25 17:42 +0200
    Re: qsort() vs. std::sort Juha Nieminen <nospam@thanks.invalid> - 2022-04-26 05:26 +0000
      Re: qsort() vs. std::sort Öö Tiib <ootiib@hot.ee> - 2022-04-26 00:19 -0700
      Re: qsort() vs. std::sort scott@slp53.sl.home (Scott Lurndal) - 2022-04-26 14:03 +0000
        Re: qsort() vs. std::sort Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-04-26 08:50 -0700
        Re: qsort() vs. std::sort Öö Tiib <ootiib@hot.ee> - 2022-04-29 01:38 -0700
          Re: qsort() vs. std::sort Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-04-29 02:39 -0700
            Re: qsort() vs. std::sort Öö Tiib <ootiib@hot.ee> - 2022-04-29 04:06 -0700
    Re: qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-26 12:28 +0200
      Re: qsort() vs. std::sort Öö Tiib <ootiib@hot.ee> - 2022-04-26 04:54 -0700
        Re: qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-26 14:10 +0200
          Re: qsort() vs. std::sort Öö Tiib <ootiib@hot.ee> - 2022-04-26 06:23 -0700
            Re: qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-26 16:26 +0200
  Re: qsort() vs. std::sort William Ahern <william@25thandClement.com> - 2022-04-27 17:03 -0700
    Re: qsort() vs. std::sort Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-28 06:58 +0200
    Re: qsort() vs. std::sort Juha Nieminen <nospam@thanks.invalid> - 2022-04-28 06:33 +0000
      Re: qsort() vs. std::sort Öö Tiib <ootiib@hot.ee> - 2022-04-28 01:08 -0700
        Re: qsort() vs. std::sort Juha Nieminen <nospam@thanks.invalid> - 2022-04-28 08:23 +0000
      Re: qsort() vs. std::sort William Ahern <william@25thandClement.com> - 2022-04-28 02:55 -0700

csiph-web