Skip to content

Instantly share code, notes, and snippets.

@luiscoms
Created July 28, 2017 13:12
Show Gist options
  • Save luiscoms/23b9704ce27fc6f86202e726cd834771 to your computer and use it in GitHub Desktop.
Save luiscoms/23b9704ce27fc6f86202e726cd834771 to your computer and use it in GitHub Desktop.
Python performance test
import timeit
black_list = ["status", "components", "exposed_id", "external_id", "gaucha_id", "publish_scheduled",
"created", "origin_link", "canonical", "friendly_title", "friendly_title_history"]
input_data = {
"x": 1,
"_id": 1,
"_created": 1,
"_updated": 1,
"status": 1,
"y": 1,
}
def remove_fields_from_article(data, black_list):
new_article = {}
for key in data:
if not key.startswith('_') and key not in black_list:
new_article[key] = data[key]
return new_article
print(timeit.timeit('remove_fields_from_article(input_data, black_list)', globals=globals()))
# 1.7095878859981894
def remove_fields_from_article(data, black_list):
new_article = {}
keys = list(filter(lambda key: not key.startswith('_') and key not in black_list, list(data.keys())))
for key in keys:
new_article[key] = data[key]
return new_article
print(timeit.timeit('remove_fields_from_article(input_data, black_list)', globals=globals()))
# 3.507615328009706
def remove_fields_from_article(data, black_list):
new_article = {}
[new_article.update({key: data[key]}) for key in data if not key.startswith('_') and key not in black_list]
return new_article
print(timeit.timeit('remove_fields_from_article(input_data, black_list)', globals=globals()))
# 2.7314564289990813
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment