Skip to content

Instantly share code, notes, and snippets.

@kassane
Last active November 19, 2023 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kassane/d65b0fffdc8ef18b13dd7baa31bc7527 to your computer and use it in GitHub Desktop.
Save kassane/d65b0fffdc8ef18b13dd7baa31bc7527 to your computer and use it in GitHub Desktop.
slsort C++
#include <iostream>
#include <chrono>
#include <array>
#include <thread>
#include <asio/thread_pool.hpp>
#include <asio/post.hpp>
void sleepSort(const std::array<int,8>& numbers) {
asio::thread_pool pool(numbers.size());
for (const auto& num : numbers) {
asio::post(pool, [num]() {
std::this_thread::sleep_for(std::chrono::milliseconds(num));
std::cout << num << " ";
});
}
// Block until all tasks are complete
pool.join();
std::cout << std::endl;
}
int main() {
std::array<int,8> numbers = {9, 40, 10, 1, 6, 45, 23, 50};
for (const auto& num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
std::cout << "Sorted numbers: ";
sleepSort(numbers);
return 0;
}
#include <iostream>
#include <chrono>
#include <thread>
#include <vector>
void sleepSort(const std::vector<int>& numbers) {
std::vector<std::thread> threads;
for (const auto& num : numbers) {
threads.emplace_back([num]() {
std::this_thread::sleep_for(std::chrono::milliseconds(num));
std::cout << num << " ";
});
}
for (auto& thread : threads) {
thread.join();
}
std::cout << std::endl;
}
int main() {
std::vector<int> numbers = {9, 40, 10, 1, 6, 45, 23, 50};
for (const auto& num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
std::cout << "Sorted numbers: ";
sleepSort(numbers);
return 0;
}
import std.algorithm: map, each;
import core.thread: Thread, dur;
import std.stdio: write, writeln;
import std.conv: to;
import std.parallelism: parallel;
void sleepSort(immutable (uint[]) numbers) {
numbers[0..$].map!(a => a.to!uint).parallel.each!((a) {
Thread.sleep(dur!"msecs"(a));
write(a, " ");
});
writeln();
}
void main() {
writeln("Sleep sorting");
immutable (uint[]) numbers = [9, 40, 10, 1, 6, 45, 23, 50];
writeln("Original numbers: ");
foreach (num; numbers) {
write(num, " ");
}
writeln("\nSorted numbers: ");
sleepSort(numbers);
}
@kassane
Copy link
Author

kassane commented Jun 17, 2023

Zig version: https://gist.github.com/kassane/b4afca62636fe08b950691d190fce5ac

$> poop './slsort-cpp' './slsort-zig'
Benchmark 1 (96 runs): ./slsort-cpp:
  measurement      mean ± σ               min … max                  delta
  wall_time        51.842ms ± 204.779us   51.496ms … 52.34ms         0%
  peak_rss         3M ± 126K              3M … 3M                    0%
  cpu_cycles       2503498 ± 283200       1790151 … 3196913          0%
  instructions     2582295 ± 99           2582256 … 2583147          0%
  cache_references 77042 ± 1897           67908 … 84641              0%
  cache_misses     29748 ± 639            28067 … 31928              0%
  branch_misses    15813 ± 65             15658 … 15969              0%
Benchmark 2 (98 runs): ./slsort-zig:
  measurement      mean ± σ               min … max                  delta
  wall_time        51.063ms ± 176.721us   50.792ms … 51.737ms        ⚡-1.5%
  peak_rss         788K ± 290K            729K … 2M                  ⚡-78.6%
  cpu_cycles       121249 ± 14284         84985 … 170869             ⚡-95.2%
  instructions     11301 ± 99             11107 … 11618              ⚡-99.6%
  cache_references 7440 ± 127             7150 … 7780                ⚡-90.3%
  cache_misses     2965 ± 249             2269 … 3326                ⚡-90.0%
  branch_misses    546 ± 14               512 … 579                  ⚡-96.5%

@kassane
Copy link
Author

kassane commented Jun 17, 2023

C++: STD vs Asio

Benchmark 1 (96 runs): ./slsort-cpp:
  measurement      mean ± σ               min … max                  delta
  wall_time        51.818ms ± 193.634us   51.375ms … 52.496ms        0%
  peak_rss         3M ± 127K              3M … 3M                    0%
  cpu_cycles       2465555 ± 240697       1742544 … 2848205          0%
  instructions     2582316 ± 89           2582283 … 2583075          0%
  cache_references 77329 ± 1816           68583 … 89089              0%
  cache_misses     29872 ± 626            28468 … 31652              0%
  branch_misses    15819 ± 85             15543 … 16323              0%
Benchmark 2 (96 runs): ./slsort-asio:
  measurement      mean ± σ               min … max                  delta
  wall_time        52.18ms ± 314.125us    51.78ms … 54.142ms           +0.7%
  peak_rss         3M ± 103K              3M … 3M                    💩+1.8%
  cpu_cycles       2784577 ± 323426       2025760 … 3798170          💩+12.9%
  instructions     2648660 ± 2468         2644055 … 2653782          💩+2.6%
  cache_references 87856 ± 2523           80286 … 100747             💩+13.6%
  cache_misses     35700 ± 1503           32188 … 43450              💩+19.5%
  branch_misses    17239 ± 87             17028 … 17560              💩+9.0%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment