'Django runserver performance of functions that do not make requests
I have found plenty of questions on here that address the performance of runserver, but all of those were with regards to the performance of requests and responses. My question is about performance of methods of a class that are executed from views.
I am quite new to Django, so forgive me for mistakes in general Django practices and let's try to focus on my question.
Simplified for sake of clarity, my current project looks like this:
search
├── manage.py
├── retrieval
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── retrieval_execution
│ │ └── retrieval_execution.py
│ ├── urls.py
│ └── views.py
├── core
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── test.py
In retrieval_execution.py, I have the following methods that are being executed through views.py:
def delta_decoder(self, delta_encoded_inverted_list):
"""
input params:
v_byte_encoded_inverted_list : dictionary
one key being the word, and values a list with delta encoded doc_id and decoded positions
return:
inverted list in its original format {word: [document_count, [[doc_number, [positions]]]}
"""
doc_count, delta_pos_combos = delta_encoded_inverted_list # int, list
list_out = [doc_count, {}]
# add the first doc number manually
current_doc_num, positions = delta_pos_combos[0] # int, list
list_out[1][current_doc_num] = positions # add doc and pos to doc_pos dict
for delta_pos_combo in delta_pos_combos[1:]:
delta, positions = delta_pos_combo
current_doc_num = current_doc_num + delta
list_out[1][current_doc_num] = positions
return list_out
def mini_index_builder(self):
self.mini_index = {}
start_time = datetime.datetime.now()
for word in self.pre_processed_query:
if word in self.inverted_index:
decoded_list = self.delta_decoder(self.inverted_index[word])
self.mini_index[word] = decoded_list
print(f"building the mini index and decoding took {datetime.datetime.now() - start_time}")
# check if mini_index is valid (at least one word of query is in the index)
return self.valid_index()
def valid_index(self):
"""
If the index does not return any documents,
we can instantly return a page saying there
are no results for the input query.
"""
if len(self.mini_index.keys()) == 0:
return False
else:
return True
To test the code locally from my terminal, I created the test.py file on base directory level, which executes the code above the same way as views.py does.
Now my question is: I noticed that the time that the execution of the piece of code takes is about twice as long when I execute through views.py on runserver, than when I run the test.py locally (I know datetime.now() is not an ideal way to test runtime, but this was the quickest way I could come up with). I've tried multiple times with multiple different inputs and the results are consistent. This actually really surprised me, and purely out of curiosity I was wondering if someone could explain why this takes much more time using runserver.
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
