最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to Reduce time when formatting the Cypher result? - Stack Overflow

programmeradmin3浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

Your 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)
发布评论

评论列表(0)

  1. 暂无评论