Created
December 16, 2018 14:39
-
-
Save npetrenko/359c522c741af497e4e3f73254b4ffba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bisect | |
import random | |
import pytest | |
import numpy as np | |
from quantize import quantize | |
from time import time | |
def quantize_slow(x, n): | |
values = sorted(list(x)) | |
boundaries = [values[len(x) * p // n] for p in range(n)] | |
quantized = [bisect.bisect_left(boundaries, x[i]) for i in range(len(x))] | |
return np.array(boundaries), np.array(quantized) | |
def run_test(quantizer, arr, nq): | |
boundaries, quantized = quantizer(arr, 50) | |
if __name__ == "__main__": | |
setups = [(7*x, x) for x in range(10, 2000, 500)] | |
for arr_size, nq in setups: | |
arr = np.random.normal(size=arr_size).astype(np.float32) | |
print("\nSetup: arr_size " + str(arr_size) + " nq " + str(nq)) | |
benchs = {quantize : "C++", quantize_slow : "Python"} | |
for alg, lang in benchs.items(): | |
print(lang + ":") | |
total = 0. | |
num_runs = 100000//nq | |
for _ in range(num_runs): | |
start = time() | |
run_test(alg, arr, nq) | |
stop = time() | |
total += stop - start | |
print(total) | |
print("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment