Skip to content

Commit fec5e5e

Browse files
committed
hero demo snippets
Signed-off-by: Saurabh Misra <[email protected]>
1 parent 159867a commit fec5e5e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

codeflash/demo_snippets.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# ORIGINAL SLOW CODE
2+
3+
4+
def find_common_tags(news_articles) -> set[str]:
5+
if not news_articles:
6+
return set()
7+
8+
common_tags = news_articles[0]["tags"]
9+
for article in news_articles[1:]:
10+
common_tags = [tag for tag in common_tags if tag in article.get("tags", [])]
11+
return set(common_tags)
12+
13+
14+
# OPTIMIZATION 1: Does not win - Is 33x faster
15+
16+
17+
def find_common_tags(news_articles) -> set[str]:
18+
if not news_articles:
19+
return set()
20+
21+
common_tags = set(news_articles[0]["tags"])
22+
for article in news_articles[1:]:
23+
common_tags &= set(article.get("tags", []))
24+
return common_tags
25+
26+
27+
# OPTIMIZATION 2 : Winning Optimization - Is 90x faster
28+
29+
30+
def find_common_tags(news_articles) -> set[str]:
31+
if not news_articles:
32+
return set()
33+
34+
common_tags = set(news_articles[0]["tags"])
35+
for article in news_articles[1:]:
36+
common_tags.intersection_update(article.get("tags", []))
37+
return common_tags

0 commit comments

Comments
 (0)