GithubHelp home page GithubHelp logo

gerben-s / quicksort-blog-post Goto Github PK

View Code? Open in Web Editor NEW
39.0 4.0 7.0 96 KB

Home Page: https://blog.reverberate.org/2020/05/29/hoares-rebuttal-bubble-sorts-comeback.html

License: Apache License 2.0

Starlark 2.30% C++ 97.70%
quicksort blogpost

quicksort-blog-post's Introduction

Branchfree QuickSort performance analysis

This is not an officially supported Google product

This directory contains code and benchmarks accompanying the blog post about QuickSort performance.

The main() function validates the correctness of the main algorithms before running benchmarks.

The benchmark requires Bazel to build. At build time Bazel will automatically download and build the benchmarking framework.

From this directory, run:

$ CC=clang bazel build -c opt :bench_sort
$ ../bazel-bin/quicksort-blog-post/bench_sort

The versions of gcc I've run did not lower conditionals into branchfree code.

Overview of benchmarks

Random int's

Benchmark                                          Time           CPU Iterations
---------------------------------------------------------------------------------
BM_Sort<std::sort>                                79 ns         78 ns    9300000
BM_Sort<std::stable_sort>                         90 ns         90 ns    7400000
BM_Sort<std_heap_sort>                           130 ns        130 ns    4600000
BM_Sort<andrei::sort>                             52 ns         52 ns   15100000
BM_Sort<exp_gerbens::QuickSort>                   30 ns         30 ns   24700000
BM_Sort<pdqsort>                                  42 ns         42 ns   16800000
BM_Sort<HeapSort>                                 51 ns         51 ns   14500000

Random pointers sort on address (0 levels of indirection)

Benchmark                                              Time           CPU Iterations
---------------------------------------------------------------------------------
BM_IndirectionSort<0, std::sort>                      77 ns         77 ns    9200000
BM_IndirectionSort<0, std::stable_sort>               92 ns         91 ns    7600000
BM_IndirectionSort<0, std_heap_sort>                 124 ns        124 ns    5800000
BM_IndirectionSort<0, andrei::sort>                   56 ns         56 ns   10000000
BM_IndirectionSort<0, exp_gerbens::QuickSort>         32 ns         32 ns   18300000
BM_IndirectionSort<0, pdqsort_branchless>             40 ns         40 ns   17600000
BM_IndirectionSort<0, HeapSort>                       60 ns         60 ns   11900000

Random pointers sort on value pointed to (1 levels of indirection)

Benchmark                                              Time           CPU Iterations
-------------------------------------------------------------------------------------
BM_IndirectionSort<1, std::sort>                      97 ns         97 ns    7400000
BM_IndirectionSort<1, std::stable_sort>              133 ns        133 ns    5100000
BM_IndirectionSort<1, std_heap_sort>                 180 ns        180 ns    4100000
BM_IndirectionSort<1, andrei::sort>                   67 ns         67 ns   11600000
BM_IndirectionSort<1, exp_gerbens::QuickSort>         42 ns         42 ns   16300000
BM_IndirectionSort<1, pdqsort_branchless>             54 ns         54 ns   10000000
BM_IndirectionSort<1, HeapSort>                      131 ns        131 ns    6000000

quicksort-blog-post's People

Contributors

gerben-s avatar gerben-stavenga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

quicksort-blog-post's Issues

Quadsort as another idea to benchmark against

At the end of your blog post you've even tried implementing CPU-pipeline-efficient MergeSort. What about enhancing Quadsort (which is MergeSort, but with a tiny sorting network built-in) by your DistributeBackward and DistributeForward approach?

Bad behavior if all elements of array are equal

I pulled this into my code to see how it compared to a sort implementation I was using and found that the tests were hanging. After a little investigation, it wasn't hanging but was just performing very badly for the cases where all elements are equal. Looks like it partiions into subarrays of size 1 and n-1 and continues that all the way down or until it blows up due to too much recursion.

I haven't looked at any fixes, but wanted to know whether that was a known issue with the experimental code or whether that behavior had been overlooked.

Although the test is easy to reproduce, here is the code for completeness:

#include <hybrid_qsort.h>
#include <vector>
int main()
{
  //  std::vector<int> v(131071,1);
  std::vector<int> v(1024,1);
  exp_gerbens::QuickSort(v.data(), v.size());

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.