Created
April 27, 2020 13:17
-
-
Save cidrblock/3fb4549c288adec8465a40b10d879c38 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # (venv) ➜ argspec_test python go.py | |
| # Estimated file length is 3121 | |
| # Loading python file took 0.1354 seconds | |
| # Loading yaml(python parser) file took 4.1489 seconds | |
| # Loading yaml(C parser) file took 0.4825 seconds | |
| # (venv) ➜ argspec_test | |
| import contextlib | |
| import csv | |
| import json | |
| import pprint | |
| import time | |
| import urllib.request | |
| import yaml | |
| URL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/04-23-2020.csv" | |
| def build_sources(): | |
| with contextlib.closing(urllib.request.urlopen(URL)) as fp: | |
| raw_bytes = fp.read() | |
| data_str = raw_bytes.decode("utf8") | |
| print("Estimated file length is %s" % len(data_str.splitlines())) | |
| dump = {'data': [dict(row) for row in csv.DictReader(data_str.splitlines())]} | |
| with open('data.yaml', 'w') as file: | |
| yaml.dump(dump, file) | |
| dump = "DATA = " + pprint.pformat(dump, indent=4) | |
| with open("data.py", "w") as file1: | |
| file1.writelines(dump) | |
| def time_python(): | |
| tic = time.perf_counter() | |
| from data import DATA as laoded | |
| toc = time.perf_counter() | |
| py_elapsed = toc - tic | |
| print(f"Loading python file took {py_elapsed:0.4f} seconds") | |
| def time_yamlp(): | |
| tic = time.perf_counter() | |
| with open('data.yaml') as f: | |
| data = yaml.load(f, Loader=yaml.SafeLoader) | |
| toc = time.perf_counter() | |
| yaml_elasped = toc - tic | |
| print(f"Loading yaml(python parser) file took {yaml_elasped:0.4f} seconds") | |
| def time_yamlc(): | |
| tic = time.perf_counter() | |
| with open('data.yaml') as f: | |
| data = yaml.load(f, Loader=yaml.CLoader) | |
| toc = time.perf_counter() | |
| yaml_elasped = toc - tic | |
| print(f"Loading yaml(C parser) file took {yaml_elasped:0.4f} seconds") | |
| if __name__ == "__main__": | |
| build_sources() | |
| time_python() | |
| time_yamlp() | |
| time_yamlc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment