Hello GNU profiling

The profiling tool in GNU is called gprof. Here is a short, boring example of how to use it.

1) Write hello world in C (hello.c)

#include <stdio.h>
 
int foo() {
  int b = 54324;
  int j;
  for (j=0; j < 1000000; j++) {
    b = b^j;
  }
  return b;
}
 
int main() {
  int a = 321782;
  int i;
  for(i=0; i<1000; i++) {
    a = a ^ foo();
  }
  printf("Hello foo: %d\n", a);
  return 0;
}
 
}

2) Compile with -pg option

gcc -pg hello.c

3) Run the program to generate profiling information

./a.out # this generates gmon.out file

4) Run gprof on the program and read output in less:

gprof a.out gmon.out | less

A stop watch for Postgres

To time the execution of various stages of a long transaction, I’m using the following function:

CREATE OR REPLACE FUNCTION CVL_TimerLap() RETURNS double precision AS $$
import time
now = time.time()
if not SD.has_key('t_last'):
  SD['t_last'] = now
elapsed = now - SD['t_last']
SD['t_last'] = now
return elapsed
$$ LANGUAGE plpythonu;

The “lap times” are returned using:

SELECT CVL_TimerLap();

This will return the number of seconds (wall-clock time) that have passed since the function was last invoked.

Running LP-solver in Postgres

Having reinstalled PostgreSQL with support for Python and pointing at my non-system python, it is time to test whether I can use the convex optimizer library I’ve installed in my Python 2.7 (pip install cvxopt).

Install PL/Python if not already installed

-- if not already installed. Doesn't hurt.
CREATE extension plpythonu;

Create a function that imports cvxopt:

CREATE OR REPLACE FUNCTION hello_cvxopt()
  RETURNS text
AS $$
  import cvxopt
  RETURN cvxopt.__doc__
$$ LANGUAGE plpythonu IMMUTABLE;

See if it works:

SELECT hello_cvxopt();
-- should return a documentation string

Try the linear programming example:

CREATE OR REPLACE FUNCTION cvxopt_lp_example()
  RETURNS FLOAT[]
AS $$
  FROM cvxopt import matrix, solvers
  A = matrix([ [-1.0, -1.0, 0.0, 1.0], [1.0, -1.0, -1.0, -2.0] ])
  b = matrix([ 1.0, -2.0, 0.0, 4.0 ])
  c = matrix([ 2.0, 1.0 ])
  solvers.options['show_progress'] = FALSE
  sol=solvers.lp(c,A,b)
  RETURN list(sol['x'])
$$ LANGUAGE plpythonu IMMUTABLE;
 
SELECT cvxopt_lp_example();
-- should return something like "{0.499999995215,1.49999999912}"

Try

A new side to Bill Gates

I don’t know why, but I really liked to read this post by Bill Gates. In an odd way it was heart warming 🙂

Three Things I’ve Learned From Warren Buffett

Maybe I should start reading Bill Gates blog as well, just for more of that feel good vibe I got from reading the blog post. I not being sarcastic.

Everything is relative, especially the news value of things, so this is of course only news to me. Having been too busy with life to sit down and quietly read a post that is non-technical.

Make a RAM disk on Mac OS X 1.5 or later

Create the RAM disk:

diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://1165430`

(the magic number 1165430 is explained here as the number of 512 byte sectors. The string “ramdisk” is just the name of the disk, which will appear under /Volumes after creation)

Write some stuff to the RAM disk:

echo "hello RAM disk" > /Volumes/ramdisk/hello.txt

Eject the RAM disk:

diskutil eject /Volumes/ramdisk

Gathered from here and here so that I remember for later.

On Linux

Found this guide for creating a RAM disk on Linux, but have not tried it yet.

Calling a Python LP-solver from a PostgreSQL function

Prepare the database by installing PL/Python and a function:

CREATE EXTENSION IF NOT EXISTS plpythonu;
 
CREATE OR REPLACE FUNCTION hello_lp()
  RETURNS FLOAT[]
AS $$
  import subprocess
  output = subprocess.Popen (
    [
     "/Library/Frameworks/Python.framework/Versions/2.7/bin/python", 
     "/usr/local/trystuff/hello_lp.py"
    ], 
    stdout = subprocess.PIPE, 
    stderr = subprocess.STDOUT
  ).communicate()[0]
  # Parse string OF comma-separated floats
  RETURN map(lambda x: FLOAT(x), output.split(","))
$$ LANGUAGE plpythonu;

Install LP-solver (cvxopt):

pip install cvxopt

External Python script (save as /usr/local/trystuff/hello_lp.py):

from cvxopt import matrix, solvers
 
solvers.options['show_progress'] = False
 
A = matrix([ [-1.0, -1.0, 0.0, 1.0], [1.0, -1.0, -1.0, -2.0] ])
b = matrix([ 1.0, -2.0, 0.0, 4.0 ])
c = matrix([ 2.0, 1.0 ])
sol=solvers.lp(c,A,b)
 
# Print string of comma-separated floats
print ",".join([str(x) for x in sol['x']])

Try it:

SELECT unnest(hello_lp());

Surviving a startup with small children in the house

The question: Is it possible to be an entrepreneur while having small children and a wife? Here is what a bunch of entrepreneurs say about that:

Entrepreneur      Verdict

Jason Roberts
Pasadena
He says yes. Has three kids aged 6, 4 and 2 and several startups behind him or in the works, including AppIgnite.

Jason Calacanis
Los Angeles
He says yes, but not if it is your first startup. Has one kid, perhaps aged 2. Is a well-known entrepreneur with several startups behind him.

Gini Dietrich
She says no, has no children. Has at least two startups behind her.

Jessica Stillman
She says yes, if you are willing to give up a certain bond with your kids (between the lines, is it worth it?). The post is actually more a resume of what other people have said, so open it and follow the links.