You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The main reason there is a custom loader in this library is to replace integer keys into strings.
However, the json library from python could handle this for you.
It is in fact faster to use the original yaml loaders in combination with json dumping and loading than using a custom loader.
And if the LibYAML package is installed it's even factor 9-10 faster.
(note you have to edit the paths if you want to run the below)
importjsonfromyamlimportSafeLoaderfromyamlimportCSafeLoaderfromyamlimportloadfromopenapi_spec_validator.loadersimportExtendedSafeLoaderdefread_yaml_file(path, loader=ExtendedSafeLoader):
"""Open a file, read it and return its contents."""withopen(path) asfh:
returnload(fh, loader)
defread_yaml_file_fast(path, loader=SafeLoader):
"""Open a file, read it and return its contents."""withopen(path) asfh:
returnjson.loads(json.dumps(load(fh, loader)))
defread_yaml_file_faster(path, loader=CSafeLoader):
"""Open a file, read it and return its contents."""withopen(path) asfh:
returnjson.loads(json.dumps(load(fh, loader)))
if__name__=='__main__':
importtimeitresult=timeit.timeit(
"read_yaml_file('EDIT/openapi-spec-validator/tests/integration/data/v3.1/petstore.yaml')",
"from __main__ import read_yaml_file",
number=1000
)
print("original:", result)
result=timeit.timeit(
"read_yaml_file_fast('EDIT/openapi-spec-validator/tests/integration/data/v3.1/petstore.yaml')",
"from __main__ import read_yaml_file_fast",
number=1000
)
print("+json:", result)
result=timeit.timeit(
"read_yaml_file_faster('EDIT/openapi-spec-validator/tests/integration/data/v3.1/petstore.yaml')",
"from __main__ import read_yaml_file_faster",
number=1000
)
print("cloader + json:", result)
My 3+MB file takes 9.2 seconds to load with the original code. Versus 1,5seconds with the Cloader.
And with debugging mode on, this becomes 40+ seconds with original versus 4 seconds with Cloader.
Uh oh!
There was an error while loading. Please reload this page.
The main reason there is a custom loader in this library is to replace integer keys into strings.
However, the
json
library from python could handle this for you.It is in fact faster to use the original yaml loaders in combination with json dumping and loading than using a custom loader.
And if the LibYAML package is installed it's even factor 9-10 faster.
(note you have to edit the paths if you want to run the below)
My 3+MB file takes 9.2 seconds to load with the original code. Versus 1,5seconds with the Cloader.
And with debugging mode on, this becomes 40+ seconds with original versus 4 seconds with Cloader.
See PR: #146
The text was updated successfully, but these errors were encountered: