Skip to content

Instantly share code, notes, and snippets.

@noel-yap
noel-yap / latency.md
Last active September 9, 2024 23:32
latency.md

Latency Comparison Numbers (~2020 from colin-scott.github.io/personal_website/research/interactive_latency.html)

L1 cache reference                           1 ns
Branch mispredict                            3 ns
L2 cache reference                           4 ns
Mutex lock/unlock                           17 ns
Main memory reference                      100 ns
Read 1 MB sequentially from memory       3,000 ns        3 µs
Compress 1K bytes with Zippy 2,000 ns 2 µs
#!/bin/bash
set -e
set -o pipefail
set -u
aoeu=a/o/e/u
echo "aoeu=${aoeu}" # aoeu=a/o/e/u
echo "aoeu%/*=${aoeu%/*}" # aoeu%/*=a/o/e
echo "aoeu%%/*=${aoeu%%/*}" # aoeu%%/*=a
setup() {
. "${BATS_TEST_DIRNAME}/bash-inject.shlib"
}
@test "should default to defining function" {
assert_equal "$(declare -F fn)" ''
@inject "${BATS_TEST_DIRNAME}/fn.sh"
run echo "$(declare -f fn)"
#!/usr/bin/python3.8
import argparse
import math
def memoize(f):
memo = {}
#!/usr/bin/python3
import argparse
def sum_of_sequence(n):
return n * (n + 1) / 2
def sum_of_squares_of_sequence(n):
#!/usr/bin/python3
from typing import TextIO
try:
triangle: TextIO = open('triangle.txt')
tree = [[int(s) for s in line.split()]
for line in [line.rstrip() for line in triangle.readlines()]]
#!/usr/bin/python3
from collections import Counter
from enum import Enum
from functools import total_ordering
from typing import List, TextIO, Dict
@total_ordering
class Card:
class Triple {
public final int a;
public final int b;
public final int c;
private Triple(final int a, final int b, final int c) {
this.a = a;
this.b = b;
this.c = c;
}
enum Coin {
ONE_P(1),
TWO_P(2),
FIVE_P(5),
TEN_P(10),
TWENTY_P(20),
FIFTY_P(50),
ONE_L(100),
TWO_L(200);
public class Solution {
private Map<Integer, Integer> factorialMemo = new ConcurrentHashMap<Integer, Integer>();
private int factorial(final int n) {
final Integer f = factorialMemo.get(n);
if (f != null) {
return f;
}
if (n == 0) {