I am comparing the performance between Julia and C++. Then I found quick sort is much faster in Julia (and even faster than C++), especially when the size of array is very big.
Can anyone explain the reasons?
quickSort.jl
include("../dimension.jl")
function execute()
n = getDimension()
print(stderr, "Julia,quickSort_optim,$n,"); # use default delimiter
arr = zeros(Int32, n)
for i = 1:n
arr[i] = (777*(i-1)) % 10000
end
if n > 0
sort!(arr; alg=QuickSort)
end
end
# executing ...
execute()
quickSort_boost.cpp
#include "dimension.h"
#include <boost/lambda/lambda.hpp>
#include <boost/sort/pdqsort/pdqsort.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
using namespace boost::sort;
int main()
{
int n = getDimension();
cerr << "C++,quickSort_boost," << n << ",";
vector<int> arr(n);
unsigned long long w;
for(int i = 0; i < n; ++i){ // array for sorting
w = (777*i) % 10000; // Array with values between 0 and 10000
arr[i] = w;
}
if (n > 0){
pdqsort_branchless(arr.begin(), arr.end(), [](const int &a, const int &b){return ( a < b );});
}
return 0;
}
Comparison
Note
The function getDimension() is used to get the array size.
The execution time is measured with the shell command: /usr/bin/time under Ubuntu. Compiler: clang version 6.0.0-1ubuntu2. Optimization level: -02. CPU: Intel i7-3820QM
The reason that I compared the whole execution time rather than just the algorithm itself is because I want to compare the performance between these two languages, which simulates a real application scenario.
In Julia official document, it writes: QuickSort: good performance for large collections. Is this because Julia uses a special implementation inside the algorithm.
More samples
I run the test with more samples. It seems that the distribution of the data is the issue.
- best case with broadly distributed data:
function execute() # julia code segment for specifying data
for i = 1:n
arr[i] = i
end
for(int i = 0; i < n; ++i){ // c++ boost code segment for specifying data
arr[i] = i + 1;
}
- worst case with broadly distributed data:
function execute() # julia code segment for specifying data
for i = 1:n
arr[i] = n - i + 1
end
for(int i = 0; i < n; ++i){ // c++ boost code segment for specifying data
arr[i] = n - i;
}
- Concentrated distributed data
function execute() # julia code segment for specifying data
for i = 1:n
arr[i] = i % 10
end
for(int i = 0; i < n; ++i){ // c++ boost code segment for specifying data
arr[i] = (i + 1) % 10;
}




