Skip to content

Instantly share code, notes, and snippets.

@amorton
Created October 3, 2024 18:50
Show Gist options
  • Save amorton/3ed3787bbad3f2f50fe8959984de9240 to your computer and use it in GitHub Desktop.
Save amorton/3ed3787bbad3f2f50fe8959984de9240 to your computer and use it in GitHub Desktop.
python vector benchmarking
import base64
import json
import random
import struct
import time
vector_96 = tuple(random.uniform(0.0, 1.0) for _ in range(96))
vector_96_csv = ','.join(str(x) for x in vector_96)
vector_96_json = json.dumps(vector_96)
vector_96_byte_buffer = struct.pack("96f", *vector_96)
vector_96_base64 = base64.b64encode(vector_96_byte_buffer)
def load_json_string(count: int):
for _ in range(count):
json.loads(vector_96_json)
def load_csv_string(count: int):
for _ in range(count):
[float(x) for x in vector_96_csv.split(',')]
def decode_base64(count: int):
for _ in range(count):
base64.b64decode(vector_96_base64)
def unpack_byte_buffer(count: int):
for _ in range(count):
struct.unpack("96f", vector_96_byte_buffer)
def decode_and_unpack(count: int):
for _ in range(count):
struct.unpack("96f", base64.b64decode(vector_96_base64))
def main():
results = []
for func in [load_json_string, load_csv_string, decode_base64, unpack_byte_buffer, decode_and_unpack]:
start = time.perf_counter()
func(10000)
end = time.perf_counter()
duration = end - start
results.append((func.__name__, duration))
results.sort(key=lambda x: x[1])
slowest = results[-1][1]
for name, duration in results:
print(f"{name}: \t{duration:.12f} seconds \t {duration/slowest:.2f}x")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment