Commits

Gora Khargosh committed f093de6

Adds more speed tests for simpler uint-bytes codecs.

Signed-off-by: Gora Khargosh <gora.khargosh@gmail.com>

  • Participants
  • Parent commits 4ee812a

Comments (0)

Files changed (3)

     from functools import reduce
 reduce = reduce
 
+try:
+    # Python 2.x
+    from itertools import imap as map
+except ImportError:
+    # Python 3.x
+    pass
+
 
 if getattr(dict, "iteritems", None):
     def dict_each(func, iterable):

mom/codec/_alt_integer.py

 
 from array import array
 from struct import pack, pack_into, unpack
-from mom._compat import range, ZERO_BYTE, get_word_alignment, EMPTY_BYTE
-from mom.builtins import integer_byte_length, b, byte, is_bytes
+from mom._compat import range, ZERO_BYTE, \
+    get_word_alignment, EMPTY_BYTE, \
+    map, reduce
+from mom.builtins import integer_byte_length, b, byte, is_bytes, byte_ord
 
 
 def uint_to_bytes_naive_array_based(uint, chunk_size=0):
 #        raw_bytes = raw_bytes[first_non_zero:]
 #    return raw_bytes
 #
+
+
+def uint_to_bytes_simple(num):
+    assert num >= 0
+    rv = []
+    while num:
+        rv.append(byte(num & 0xff))
+        num >>= 8
+    return EMPTY_BYTE.join(reversed(rv))
+
+
+def bytes_to_uint_simple(bytes):
+    return reduce(lambda a, b: a << 8 | b, map(byte_ord, bytes), 0)
+

run_speed_tests.py

     "from mom._alt_builtins import integer_bit_length_shift_counting; n=1<<4096",
     None,
     "from mom.codec.integer import uint_to_bytes; n=1<<4096",
+    "from mom.codec._alt_integer import uint_to_bytes_simple; n=1<<4096",
     "from mom.codec._alt_integer import uint_to_bytes_array_based; n=1<<4096",
     "from mom.codec._alt_integer import uint_to_bytes_pycrypto; n=1<<4096",
     "from mom.codec._alt_integer import uint_to_bytes_naive; n=1<<4096",
     None,
     "import os; from mom.codec.integer import bytes_to_uint; b = os.urandom(4003)",
     "import os; from mom.codec._alt_integer import bytes_to_uint_naive; b = os.urandom(4003)",
+    "import os; from mom.codec._alt_integer import bytes_to_uint_simple; b = os.urandom(4003)",
 ]
 statements = [
     "b58encode(b)",
     "integer_bit_length_shift_counting(n)",
     None,
     "uint_to_bytes(n)",
+    "uint_to_bytes_simple(n)",
     "uint_to_bytes_array_based(n)",
     "uint_to_bytes_pycrypto(n)",
     "uint_to_bytes_naive(n)",
     None,
     "bytes_to_uint(b)",
     "bytes_to_uint_naive(b)",
+    "bytes_to_uint_simple(b)",
 ]