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
#![allow(dead_code)] | |
use anyhow::Context; | |
use std::alloc::AllocError; | |
use std::alloc::{Allocator, Layout}; | |
use std::ffi::{c_char, c_uint, CStr, CString}; | |
use std::io; | |
use std::mem::MaybeUninit; | |
use std::os::fd::AsRawFd; | |
use std::process; |
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
#!/bin/bash | |
# | |
# Usage: runperf ./my-benchmark-binary | |
# | |
# Script to run a benchmark / performance test in decent conditions. Based on: | |
# - https://www.llvm.org/docs/Benchmarking.html | |
# - "Performance Analysis and Tuning on Modern CPU" by Denis Bakhvalov, Appendix A. | |
# - https://github.com/andikleen/pmu-tools | |
# | |
# Note that this doesn't do any actual benchmarking, your binary must be able to do that all by itself. |
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
use std::error::Error; | |
use std::io::{stdin, Read}; | |
use std::time; | |
fn main() -> Result<(), Box<dyn Error>> { | |
let mut v: Vec<u8> = vec![0; 65536]; | |
let mut howmany = 0; | |
let s = stdin(); | |
let mut cin = s.lock(); | |
let mut n = 1; |
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
// 1. Run it as is (SeqCst). Should be just timing output | |
// 2. Change the two HERE lines to Ordering::Relaxed and run it again. | |
// That should give lots of (seemingly impossible) memory reorderings. | |
use rand; | |
use std::sync::atomic::{AtomicU8, Ordering}; | |
use std::sync::*; | |
use std::thread; | |
fn main() { |
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
trait Position<T> { | |
fn pos(&self) -> T; | |
} | |
struct Location { | |
lat: f32, | |
lon: f32, | |
} | |
impl Position<String> for Location { |
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
trait Nameable<T> { | |
fn set_name(&mut self, T); | |
} | |
struct Cyborg{ | |
name: Option<String>, | |
} | |
impl Nameable<&str> for Cyborg { | |
fn set_name(&mut self, s: &str) { |
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
struct Sponge { | |
name: String, | |
} | |
impl std::ops::Add for Sponge { | |
type Output = Self; | |
fn add(self, other: Self) -> Self { | |
Sponge { | |
name: self.name + "X" + &other.name, | |
} |
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
trait SurelyNot { | |
fn can_we_build_it(&self); | |
} | |
impl SurelyNot for String { | |
fn can_we_build_it(&self) { | |
println!("Yes we can"); | |
} | |
} | |
fn main() { |
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
struct StdoutLogger {} | |
impl std::fmt::Display for StdoutLogger { | |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
writeln!(f, "I am StdoutLogger") | |
} | |
} | |
fn main() { | |
let l = StdoutLogger {}; |
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
trait Logger { | |
fn log(&self, s: &str); | |
fn err(&self, e: &str) { | |
self.log(e); | |
} | |
} | |
struct StdoutLogger {} | |
impl Logger for StdoutLogger { |
NewerOlder