I'm retrieving results from a Cypher query, which includes the article's date and text.
After fetching the results, I'm formatting them before passing them to the LLM for response generation. Currently, I'm using the following approach for formatting:
context_text = "\n".join(map(lambda row: f"{row['article.date']} {row['article.text']}", results))
However, this formatting step alone takes 10-15 seconds. How can I optimize this process to reduce execution time?
context_text = "\n".join(
[f"[{row['article.date']}] {row['article.text']}" for row in results]
)
Changing code to this, doesn't help either.
I'm retrieving results from a Cypher query, which includes the article's date and text.
After fetching the results, I'm formatting them before passing them to the LLM for response generation. Currently, I'm using the following approach for formatting:
context_text = "\n".join(map(lambda row: f"{row['article.date']} {row['article.text']}", results))
However, this formatting step alone takes 10-15 seconds. How can I optimize this process to reduce execution time?
context_text = "\n".join(
[f"[{row['article.date']}] {row['article.text']}" for row in results]
)
Changing code to this, doesn't help either.
Share Improve this question edited Mar 26 at 17:41 jose_bacoy 12.7k1 gold badge24 silver badges40 bronze badges asked Mar 26 at 8:23 Yuvraj Singh BhadauriaYuvraj Singh Bhadauria 1 1- 1 You either have a very slow computer or results is massive – Adon Bilivit Commented Mar 26 at 9:15
1 Answer
Reset to default 0Your first code snippet calls a lambda function for every result element, which incurs unnecessary overhead. Your second snippet uses a list comprehension, which first creates a single list with all the substrings before joining them, which incurs unnecessary overhead and uses more memory.
The following snippet should be faster (but you will have to test it out to see how much faster). It uses a generator that does not repeatedly call a function nor create a temporary list:
context_text = "\n".join(f"{row['article.date']} {row['article.text']}" for row in results)