Skip to content

Instantly share code, notes, and snippets.

View ttsugriy's full-sized avatar

Taras Tsugrii ttsugriy

  • Meta
  • Seattle, WA
View GitHub Profile
@ttsugriy
ttsugriy / strncmp_vs_strcmp_bench.cpp
Created September 24, 2023 03:54
strncmp vs strcmp benchmark
#include <string.h>
/* Compare no more than N characters of S1 and S2,
returning less than, equal to or greater than zero
if S1 is lexicographically less than, equal to or
greater than S2. */
int
STRNCMP (const char *s1, const char *s2, size_t n)
{
unsigned char c1 = '\0';
@ttsugriy
ttsugriy / reservoir_bench.cpp
Created September 9, 2023 23:25
Reservoir sampling benchmark
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
class AlgorithmL {
private:
std::vector<double> reservoir;
@ttsugriy
ttsugriy / utf8_length_from_utf32_bench.cpp
Created September 3, 2023 04:05
utf8 length from utf32 benchmark
#include <cstddef>
#include <cstdint>
#include <cuchar>
size_t utf8_length_from_utf32(const char32_t* buf, size_t len) {
// We are not BOM aware.
const uint32_t* p = reinterpret_cast<const uint32_t*>(buf);
size_t counter{0};
for (size_t i = 0; i < len; i++) {
/** ASCII **/
use criterion::{criterion_group, criterion_main, Criterion};
fn new_with_push(name: &str) -> Vec<&str> {
let mut v = Vec::new();
v.push(name);
v
}
fn new_with_macro(name: &str) -> Vec<&str> {
vec![name]
use criterion::{criterion_group, criterion_main, Criterion};
pub fn to_vec1(s: &[u32]) -> Vec<u32> {
struct DropGuard<'a> {
vec: &'a mut Vec<u32>,
num_init: usize,
}
impl<'a> Drop for DropGuard<'a> {
#[inline]
fn drop(&mut self) {
use criterion::{criterion_group, criterion_main, Criterion};
fn sort_by_words1(name: &str) -> String {
let mut split_words: Vec<&str> = name.split('_').collect();
// We are sorting primitive &strs and can use unstable sort here.
split_words.sort_unstable();
split_words.join("_")
}
fn sort_by_words2(name: &str) -> Vec<&str> {
use criterion::{criterion_group, criterion_main, Criterion};
use std::str;
pub const MAX_BASE: usize = 64;
pub const ALPHANUMERIC_ONLY: usize = 62;
pub const CASE_INSENSITIVE: usize = 36;
const BASE_64: &[u8; MAX_BASE] =
b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$";
use criterion::{criterion_group, criterion_main, Criterion};
use std::str;
pub const MAX_BASE: usize = 64;
pub const ALPHANUMERIC_ONLY: usize = 62;
pub const CASE_INSENSITIVE: usize = 36;
const BASE_64: &[u8; MAX_BASE] =
b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$";
@ttsugriy
ttsugriy / binary_search_slice.rs
Created July 30, 2023 04:18
binary_search_slice using partition_point
pub fn binary_search_slice_new<'d, E, K>(data: &'d [E], key_fn: impl Fn(&E) -> K, key: &K) -> &'d [E]
where
K: Ord,
{
let size = data.len();
let start = data.partition_point(|x| key_fn(x) < *key);
// At this point `start` either points at the first entry with equal or
// greater key or is equal to `size` in case all elements have smaller keys
// Invariant: start == size || key_fn(&data[start]) >= *key
if start == size || key_fn(&data[start]) != *key {
@ttsugriy
ttsugriy / equal_range.h
Created July 30, 2023 04:14
equal_range in LLVM
template <class _AlgPolicy, class _Compare, class _Iter, class _Sent, class _Tp, class _Proj>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter, _Iter>
__equal_range(_Iter __first, _Sent __last, const _Tp& __value, _Compare&& __comp, _Proj&& __proj) {
auto __len = _IterOps<_AlgPolicy>::distance(__first, __last);
_Iter __end = _IterOps<_AlgPolicy>::next(__first, __last);
while (__len != 0) {
auto __half_len = std::__half_positive(__len);
_Iter __mid = _IterOps<_AlgPolicy>::next(__first, __half_len);
if (std::__invoke(__comp, std::__invoke(__proj, *__mid), __value)) {
__first = ++__mid;