def test_speed(): vectors = [np.random.normal(size=(i%40+1,)) for i in xrange(40)] start1 = time.time() for vec in vectors: reference_unpack64(reference_pack64(vec)) time_reference = (time.time() - start1)*1000 start2 = time.time() for vec in vectors: unpack64(pack64(vec)) time_ours = (time.time() - start2)*1000 assert time_ours < time_reference,\ "Took %4.4f ms. Time to beat: %4.4f ms." % (time_ours, time_reference)
def test_random_vectors(): for iter in xrange(800): magnitude = pow(2., (iter-500)/10.) length = (iter % 10) + 1 vec = np.random.normal(size=(length,)) * magnitude try: reference_unpack64(reference_pack64(vec)) yield encoding_check, vec yield decoding_check, vec except (OverflowError, ValueError): # this is a vector we can't encode in the original system pass
def decoding_check(vec): encoded = reference_pack64(vec) a = reference_unpack64(encoded) b = unpack64(encoded) assert np.allclose(a, b), '%s should have decoded to %s, got %s' % (encoded, a, b)