Skip to content

Instantly share code, notes, and snippets.

@michelp
Created February 4, 2014 22:50
Show Gist options
  • Save michelp/8814035 to your computer and use it in GitHub Desktop.
Save michelp/8814035 to your computer and use it in GitHub Desktop.
import random
import ctypes
import operator
from time import time
from numpy import frombuffer, complex64, empty, mean, ctypeslib
SAMPLE_SIZE = 1*10**6
RUNS = 1
isamples = ctypes.c_byte * SAMPLE_SIZE
fsamples = ctypes.c_float * SAMPLE_SIZE
data = [isamples(*[random.randrange(0, 255) for i in xrange(SAMPLE_SIZE)]) for j in xrange(RUNS)]
def original(b):
iq = empty(len(b)//2, complex64)
iq.real, iq.imag = b[::2], b[1::2]
iq /= (255/2)
iq -= (1 + 1j)
return iq
def masse1234(b):
b_np = ctypeslib.as_array(b)
iq = empty(len(b)//2, complex64)
iq.real, iq.imag = b_np[::2], b_np[1::2]
iq /= (255/2)
iq -= (1 + 1j)
return iq
def sliceassign(b):
f = fsamples()
f[:] = b
c = frombuffer(f, complex64)
c /= (255/2)
c -= (1 + 1j)
return c
def starassign(b):
f = fsamples(*b)
c = frombuffer(f, complex64)
c /= (255/2)
c -= (1 + 1j)
return c
def run():
results = []
for test in original, masse1234, sliceassign, starassign:
times = []
for b in data:
start = time()
results.append(test(b))
times.append(time() - start)
print test.__name__, mean(times)
return results
print "starting test"
results = run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment