Last active
November 19, 2023 14:23
-
-
Save kassane/d65b0fffdc8ef18b13dd7baa31bc7527 to your computer and use it in GitHub Desktop.
slsort C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
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
Zig version: https://gist.github.com/kassane/b4afca62636fe08b950691d190fce5ac