import timeit
import profile
import pdb
import sys

def profile_run(name, second, first):
    globals = {}
    locals = {}
    exec first in globals
    print "="*20, "profiling: " + name, "="*20
    profile.runctx(second, globals, locals)

def timer_run(name, second, first):
    t = timeit.Timer(second, first)
    print name + ":", t.repeat(3, 10000)

def debug_run(name, second, first):
    print "debugging:", name
    globals = {}
    locals = {}
    exec first in globals    
    pdb.run(second, globals)

if len(sys.argv) > 1:
    if sys.argv[1] == "profile":
        f = profile_run
    elif sys.argv[1] == "debug":
        f = debug_run
    else:
        f = timer_run
else:
    f = timer_run

f("numarray-->Numeric array_if",
  "num=Numeric.array(na, copy=0)",
  "import numarray; import Numeric; na=numarray.arange(10)")

f("numarray-->Numeric fromstring",
  "num=Numeric.fromstring(na._data,typecode=na.typecode())",
  "import numarray; import Numeric; na=numarray.arange(10)")

f("Numeric-->numarray array_if",
  "na=numarray.array(num, copy=0)",
  "import numarray; import Numeric;num=Numeric.arange(10)")

f("Numeric-->numarray buffer_if",
  "na=numarray.NumArray(buffer=num,type=num.typecode(),shape=num.shape)",
  "import numarray; import Numeric; num=Numeric.arange(10)")

