Skip to content

Instantly share code, notes, and snippets.

@mikemadisonweb
mikemadisonweb / requests.go
Created August 17, 2017 07:22 — forked from huttotw/requests.go
Concurrent requests, with max retries and error handling
package main
import (
"errors"
"fmt"
"math/rand"
"sync"
"time"
)
@mikemadisonweb
mikemadisonweb / abort.go
Created August 16, 2017 14:55
Run a http.Get async with support for aborting
package main
import (
"errors"
"fmt"
"net"
"net/http"
"sync"
"time"
)
@mikemadisonweb
mikemadisonweb / concurrency.go
Created August 16, 2017 14:27 — forked from sethmcl/concurrency.go
Simple example of goroutines and channels
package main
import (
"fmt"
"net/http"
)
const maxConcurrentRequests = 100
type Response struct {
@mikemadisonweb
mikemadisonweb / sort.go
Last active July 25, 2017 14:00
Sort a slice or whatever with ability to provide sorting order (ascending/descending etc)
package main
import (
"fmt"
"sort"
)
/*
Use sort.Sort(data Interface).
Data is an object that is not necessarily a slice.
@mikemadisonweb
mikemadisonweb / latency.txt
Created July 20, 2017 09:14 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@mikemadisonweb
mikemadisonweb / exceptions-tree.php
Created June 19, 2017 13:41 — forked from mlocati/exceptions-tree.php
Throwable and Exceptions tree
<?php
if (!function_exists('interface_exists')) {
die('PHP version too old');
}
$throwables = listThrowableClasses();
$throwablesPerParent = splitInParents($throwables);
printTree($throwablesPerParent);
if (count($throwablesPerParent) !== 0) {
die('ERROR!!!');
@mikemadisonweb
mikemadisonweb / yii2_batch_insert.php
Created April 3, 2017 07:10
ActiveRecod batch insert realization in Yii2
namespace common\components;
class Model extends yii\base\Model {
/**
* Saves multiple models.
*
* @param ActiveRecord[] $models
* @return bool
*/
@mikemadisonweb
mikemadisonweb / get_memory_usage.py
Created March 20, 2017 08:37
Get python object memory usage
import sys
def get_size(obj, seen=None):
"""Recursively finds size of objects"""
size = sys.getsizeof(obj)
if seen is None:
seen = set()
obj_id = id(obj)
if obj_id in seen:
return 0
@mikemadisonweb
mikemadisonweb / human_readable_filesize.py
Created March 20, 2017 06:35
Function for human readable file size formatting
def sizeof_fmt(num, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
@mikemadisonweb
mikemadisonweb / AbstractDaemonController.php
Last active March 16, 2017 09:46
Abstract daemon class for console commands in Yii2
<?php
namespace console\controllers;
use yii\console\Controller;
use yii\helpers\Console;
/**
* Class AbstractDaemonController
* Abstract class to build daemon console commands