-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Zeutschler/dev
caching added
- Loading branch information
Showing
2 changed files
with
22 additions
and
21 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,37 @@ | ||
from nanocube import NanoCube | ||
import pandas as pd | ||
import polars as pl | ||
from timeit import timeit | ||
from pathlib import Path | ||
import os | ||
|
||
# Create a DataFrame and NanoCube | ||
file_car_prices = Path(os.path.dirname(os.path.realpath(__file__))) / "files" / "car_prices.parquet" | ||
df = pd.read_parquet(file_car_prices) | ||
nc = NanoCube(df, dimensions=['make', 'model', 'trim', 'body'], measures=['mmr'], caching=False) | ||
nc_cached = NanoCube(df, dimensions=['make', 'model', 'trim', 'body'], measures=['mmr'], caching=True) | ||
ns = NanoCube(df, dimensions=['make', 'model', 'trim', 'body'], measures=['mmr'], caching=False) | ||
|
||
# Create a Polars table | ||
df = pl.read_parquet(file_car_prices) | ||
|
||
|
||
def query_nanocube(loops=1000): | ||
value = 0 | ||
for _ in range(loops): | ||
value += nc.get('mmr', model='Optima', trim='LX', make='Kia', body='Sedan') | ||
value += ns.get('mmr', model='Optima', trim='LX', make='Kia', body='Sedan') | ||
return value | ||
|
||
def query_nanocube_cached(loops=1000): | ||
def query_polars(loops=1000): | ||
value = 0 | ||
for _ in range(loops): | ||
value += nc_cached.get('mmr', model='Optima', trim='LX', make='Kia', body='Sedan') | ||
value += df.filter(pl.col('make') == 'Kia', pl.col('model') == 'Optima', pl.col('trim') == 'LX', pl.col('body') == 'Sedan')['mmr'].sum() | ||
return value | ||
|
||
|
||
if __name__ == '__main__': | ||
ncc_time = timeit(query_nanocube_cached, number=1) | ||
pl_time = timeit(query_polars, number=1) | ||
nc_time = timeit(query_nanocube, number=1) | ||
print(f"Polars point query in {pl_time:.5f} sec.") | ||
print(f"NanoCube point query in {nc_time:.5f} sec.") | ||
print(f"NanoCube(cached) point query in {ncc_time:.5f} sec.") | ||
print(f"NanoCube(cached) is {nc_time/ncc_time:.2f}x times faster than NanoCube(uncached) on 1000x executing the same query.") | ||
print(f"NanoCube is {pl_time/nc_time:.2f}x times faster than Polars on query with 4 filters on 1 measure:") | ||
print(f"\tns.get('mmr', model='Optima', trim='LX', make='Kia', body='Sedan')") | ||
assert(query_nanocube() == query_nanocube_cached()) | ||
assert(query_nanocube() == query_polars()) |