Skip to content

Instantly share code, notes, and snippets.

@gcatlin
gcatlin / nice_number.php
Created July 7, 2010 11:38
Number rounding for chart scales
<?php
function nice_number($num, $round=false) {
$m = pow(10, floor(log10($num)));
if ($m == 0) {
return 0;
}
$f = $num / $m;
if ($round) {
@gcatlin
gcatlin / php-speedtest.sh
Created December 23, 2010 14:58
PHP Performance Testing
#!/bin/bash
php -d implicit_flush=off -r "$1"'; for ($i=0; $i<5; $i++) { $t=microtime(true); for ($j=0; $j<100000; $j++) { '"$2"'; } printf("%.6f\n", microtime(true)-$t); }'
@gcatlin
gcatlin / gist:1534617
Created December 29, 2011 15:38
qcachegrind fix
--- a/libviews/callgraphview.cpp (revision 1261034)
+++ b/libviews/callgraphview.cpp (revision 1261035)
@@ -913,7 +913,7 @@
if (s[0] != 'F')
return 0;
bool ok;
- TraceFunction* f = (TraceFunction*) s.mid(1).toUInt(&ok, 16);
+ TraceFunction* f = (TraceFunction*) s.mid(1).toULongLong(&ok, 16);
if (!ok)
return 0;
@gcatlin
gcatlin / arsync.rb
Created February 6, 2012 04:42
Automatic rsync
#!/usr/bin/env ruby
require 'rb-fsevent'
require 'open3'
ignore_dirs = ['.git', '.hg', '.svn']
def popen cmd
Open3.popen3 cmd do |stdin, stdout, stderr|
stdout.read.split("\n").each do |line|
@gcatlin
gcatlin / gist:1782206
Created February 9, 2012 19:19
Bash one-liner to generate a histogram using R
r --vanilla --slave -e 'x<-c(1,1,1,2,2,3,3,3,3);hist(x)'
@gcatlin
gcatlin / gist:1782303
Created February 9, 2012 19:36
Unix timestamp and date conversion
# On OS X, may need to install coreutils
# brew install coreutils
#
# then use gdate instead of date
# Convert Unix timestamp to date string
date -d @1193144433
# Convert date string to Unix timestamp
date -d "2007-10-23 09:00:33" +%s
@gcatlin
gcatlin / gist:1830147
Created February 14, 2012 20:35
Restart audio on OS X
# stolen from: http://snipt.net/nsmith/restart-audio-os-x/
sudo kill -9 `ps ax | grep 'coreaudio[a-z]' | awk '{print $1}'`
@gcatlin
gcatlin / gist:1836898
Created February 15, 2012 15:58
Strip trailing whitespace from files
# source: http://stackoverflow.com/questions/149057/how-to-removing-trailing-whitespace-of-all-files-recursively
find . -not \( -name .svn -prune -o -name .git -prune \) -type f -print0 | xargs -0 sed -i '' -E "s/[[:space:]]*$//"
@gcatlin
gcatlin / gist:1845095
Created February 16, 2012 14:07
Convert audio files - m4a to mp3
# source: http://stackoverflow.com/a/5057305
# finds all the files in the current directory, converts them to mp3, using
# the original file name and changing the file extension
find . | parallel ffmpeg -i {} -ab 192k -acodec libmp3lame -ac 2 {.}.mp3
@gcatlin
gcatlin / gist:1847248
Created February 16, 2012 19:43
Install specific version of Homebrew formula
brew update
brew versions FORMULA
cd `brew --prefix`
git checkout HASH Library/Formula/FORMULA.rb # use output of "brew versions"
brew install FORMULA
brew switch FORMULA VERSION
git checkout -- Library/Formula/FORMULA.rb # reset formula
## Example: Using Subversion 1.6.17
#