Skip to content

Instantly share code, notes, and snippets.

View mtdowling's full-sized avatar

Michael Dowling mtdowling

View GitHub Profile
@mtdowling
mtdowling / gist:10002415
Last active August 29, 2015 13:58
jmespath.php perf
$ bin/perf.php
php bin/perf.php
time: 0.016928ms, description: ./bin/../tests/compliance/perf/basic.json, name: foo
time: 0.031948ms, description: ./bin/../tests/compliance/perf/basic.json, name: foo.bar
time: 0.046968ms, description: ./bin/../tests/compliance/perf/basic.json, name: foo.bar.baz
time: 0.060797ms, description: ./bin/../tests/compliance/perf/basic.json, name: foo.bar.baz.bad
time: 0.145912ms, description: ./bin/../tests/compliance/perf/deep_hierarchy.json, name: j49.j48.j47.j46.j45.j44.j43.j42.j41.j40
time: 0.711918ms, description: ./bin/../tests/compliance/perf/deep_hierarchy.json, name: j49.j48.j47.j46.j45.j44.j43.j42.j41.j40.j39.j38.j37.j36.j35.j34.j33.j32.j31.j30.j29.j28.j27.j26.j25.j24.j23.j22.j21.j20.j19.j18.j17.j16.j15.j14.j13.j12.j11.j10.j9.j8.j7.j6.j5.j4.j3.j2.j1.j0
time: 0.090837ms, description: ./bin/../tests/compliance/perf/multiwildcard.json, name: foo[*].bar[*].kind
time: 0.091791ms, description: ./bin/../tests/compliance/perf/multiwildcard.json, name: foo[*].bar[0].kind
@mtdowling
mtdowling / gist:9868295
Created March 30, 2014 06:01
Array and object truthiness
<?php
@$i = $argv[1] ?: 1000;
$c = array();
$s = microtime(true);
for ($j = 0; $j < $i; $j++) {
if ($c) {}
}
echo 'Empty Array: ' . (microtime(true) - $s) . "\n";
@mtdowling
mtdowling / simple.js
Created July 25, 2013 17:13
Simple node.js server
var http = require("http");
http.createServer(function(req, res) {
req.addListener("data", function(chunk) {
// Read data chunks
});
// Called when the request completes
req.addListener("end", function() {
res.writeHead(200, "OK", { "Content-Length": 10 });
@mtdowling
mtdowling / readfuction_perf.php
Created July 23, 2013 00:52
Simple script to reproduce CURLOPT_READFUNCTION performance issue
<?php
$m = curl_multi_init();
function sendMulti($ch)
{
global $m;
curl_multi_add_handle($m, $ch);
$active = false;
@mtdowling
mtdowling / gist:4516616
Created January 12, 2013 07:42
Various methods of calculating Fibonacci numbers in PHP
<?php
/**
* Various methods of computing Fibonacci numbers in PHP
*/
class Fibonacci
{
/**
* @var array Memoization cache
* @see Fibonacci::memoized
@mtdowling
mtdowling / gist:4516558
Last active March 7, 2018 00:56
Fibonacci in PHP using anonymous functions and memoization
<?php
$fibMemo = call_user_func(function () {
$memo = array(0 => 0, 1 => 1);
$fib = function ($n) use (&$memo, &$fib) {
if (!isset($memo[$n])) {
$memo[$n] = $fib($n - 1) + $fib($n - 2);
}
return $memo[$n];
};
@mtdowling
mtdowling / gist:3877691
Created October 12, 2012 06:56
Eval XML to array
<?php
function evalXml2Array(SimpleXMLElement $xml)
{
eval('$x = ' . str_replace(array('SimpleXMLElement::__set_state(', '))'), array('', ')'), var_export($xml, true)) . ';');
return $x;
}
@mtdowling
mtdowling / gist:3877687
Created October 12, 2012 06:54
XML to array using json_encode
<?php
// Note that this will not maintain XML attributes
function jsonXml2Array(SimpleXMLElement $xml)
{
return json_decode(json_encode($xml), true);
}
function jsonXml2ArrayWithNamespacedAttributes($xml)
{
@mtdowling
mtdowling / gist:3877616
Last active September 30, 2021 06:34
Recursive XML to array function
<?php
function xml2array($xml)
{
$arr = array();
foreach ($xml->getNamespaces() + array(null) as $prefix => $namespace) {
foreach ($xml->attributes($namespace) as $key => $value) {
// Add prefixes to prefixed attributes
if (is_string($prefix)) {
@mtdowling
mtdowling / gist:3877615
Created October 12, 2012 06:24
Iterative XML to array conversion
<?php
function iXml2array($xml)
{
$result = array();
$stack = array(array(&$result, &$xml));
do {
$frame = array_pop($stack);