Skip to content

Instantly share code, notes, and snippets.

View duarten's full-sized avatar
🤷‍♂️

Duarte Nunes duarten

🤷‍♂️
View GitHub Profile
@duarten
duarten / prefetch.cc
Last active February 13, 2017 18:08
Measuring the effects of software prefetches.
#include <algorithm>
#include <atomic>
#include <chrono>
#include <iostream>
#include <random>
using namespace std::chrono_literals;
static __inline__ uint64_t rdtsc() {
uint64_t hi, lo;
@duarten
duarten / overloaded.cc
Created February 10, 2017 11:51
Overloaded lambdas
#include <functional>
#include <iostream>
#include <string>
template <typename... Fs> class Overload;
template <typename F>
class Overload<F> {
F _f;
public:
@duarten
duarten / false_sharing.c
Last active July 15, 2020 23:44
Example program to demonstrate false sharing between threads
/*
* This is an example program to demonstrate false sharing between threads.
*
* It can be compiled two ways:
* gcc -g false_sharing.c -Wall -pthread -lnuma -o false_sharing
* gcc -g false_sharing.c -Wall -pthread -lnuma -DNO_FALSE_SHARING -o no_false_sharing
*
* The -DNO_FALSE_SHARING macro reduces the false sharing.
*
* The usage is:
@duarten
duarten / spec.c
Created January 3, 2017 12:28
A trivial C unit test framework
#include <string.h>
#include <errno.h>
#include "spec.h"
static jmp_buf buf;
void
do_run_test(testfunc test, const char *name, int position, int count)
{
@duarten
duarten / pixr.cc
Created October 25, 2016 19:52
Rename files to their creation time
#include <cstdio>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <boost/filesystem.hpp>
#include <sys/stat.h>
@duarten
duarten / bits.c
Created October 25, 2016 19:50
Bit manipulation
#include "stdio.h"
/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(int x, int y) {
@duarten
duarten / Combiner.java
Created October 25, 2016 19:48
Flat combining
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
public final class Combiner {
public interface Action {
public void apply();
}
private static final class Node {
@duarten
duarten / HungVm.java
Created October 25, 2016 19:35
Hanging the JVM
public class HungVm {
public static void main(String[] argc) throws InterruptedException {
for (int i = 0; i < 100000; ++i) {
countOdds(10);
}
Thread t = new Thread(() -> {
long l = countOdds(Integer.MAX_VALUE);
System.out.println("How Odd:" + l);
});
@duarten
duarten / pixr.clj
Created October 25, 2016 19:34
Rename files to their creation time
(ns pixr
(:import [java.io File]
[java.nio.file Files FileSystems LinkOption Path StandardCopyOption]
[java.time LocalDateTime ZoneOffset]
[java.time.temporal ChronoField]
[java.util.concurrent TimeUnit]))
(defn niopath [path]
(.getPath (FileSystems/getDefault) path (into-array String [])))
@duarten
duarten / playground.hs
Created October 25, 2016 19:33
Haskell playground
import qualified Data.Map as Map
import Data.List
import Data.List.Split
import Data.Monoid
import Control.Applicative
import System.Environment
import System.Directory
import System.IO
--module Main where