Here are some benchmarks of std.algorithm.sort vs. GCC 4.1.2's implementation of STL sort() and the implementation I use in my dstats library for lots of statistical calculations.<br><br>D code:<br>import std.stdio, std.perf, std.random, std.algorithm, dstats.sort;<br>
<br>void main(string[] args) {<br> auto nums = new float[1_000_000];<br> foreach(ref num; nums) {<br> num = uniform(0.0, 10_000_000.0);<br> }<br><br> auto pc = new PerformanceCounter;<br> pc.start;<br>
<br> if(args.length > 1 && args[1] == "--dstats") {<br> dstats.sort.qsort(nums);<br> } else {<br> std.algorithm.sort(nums);<br> }<br><br> pc.stop;<br> writeln(pc.milliseconds);<br>
}<br><br>C++ code:<br><br>#include <vector><br>#include <ctime><br>#include <cstdlib><br>#include <algorithm><br>#include <iostream><br><br>using namespace std;<br><br>// Generates quick and dirty random numbers.<br>
float sloppyRand() {<br> unsigned num = 0;<br> num += (rand() << 16);<br> num += rand();<br> return num;<br>}<br><br>int main() {<br> vector<float> nums(1000000);<br> for(int i = 0; i < nums.size(); i++) {<br>
nums[i] = sloppyRand();<br> }<br><br> double startTime = clock();<br> sort(nums.begin(), nums.end());<br> double stopTime = clock();<br><br> double clocksPerMillisec = CLOCKS_PER_SEC / 1000.0;<br> cout << (stopTime - startTime) / clocksPerMillisec << endl;<br>
}<br><br>Compilers: <br>DMD 2.047 (for D)<br>GCC 4.1.2 (For C++; I couldn't get the C++ code to compile on DMC because of STL issues that I don't feel like solving, even though this would level the playing field because D and C++ would have the same backend)<br>
<br>Results on Indel Xeon x5472:<br><br>Compiler settings: -O -inline -release (for D), -O3 (for GCC)<br><br>D, using std.algorithm.sort: 330 milliseconds<br>D, using dstats.qsort: 96 milliseconds<br>C++, using 64-bit compile: 90 milliseconds<br>
C++, using 32-bit compile: 100 milliseconds<br><br>I'd say std.algorithm.sort could use some serious optimization.<br>