From 9ea461ccc412b83298f60cc142e0599813c94e7e Mon Sep 17 00:00:00 2001 From: Radovan Bast Date: Wed, 23 Oct 2024 23:17:55 +0200 Subject: [PATCH] also archive the html version for future --- content/profiling/exercise2.html | 99 ++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 content/profiling/exercise2.html diff --git a/content/profiling/exercise2.html b/content/profiling/exercise2.html new file mode 100644 index 00000000..7a3e0ab8 --- /dev/null +++ b/content/profiling/exercise2.html @@ -0,0 +1,99 @@ + + + + + + + +
                                Memory usage: ▃▃▃█▃▃▅▅▇▇██▆▄▂▁ (max: 43.134 MB, growth rate:   0%)
+                         example.py: % of time = 100.00% (19.611s) out of 19.611s.
+       ╷       ╷       ╷       ╷        ╷       ╷               ╷       ╷
+       Time   –––––– –––––– Memory  –––––– –––––––––––    Copy                                                            
+  Line Python native system Python  peak   timeline/%     (MB/s) example.py                        
+╺━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
+     1                                                           import re                                               
+     2                                                                                                                   
+     3                                                                                                                   
+     4                                                           def count_unique_words1(file_path: str) -> int:         
+     5                                                               with open(file_path, "r", encoding="utf-8") as file:
+     6            1%         100%      13M ▁  18%                        text = file.read()                              
+     7     1%                100%      30M ▇▂▄▅▇  82%                words = re.findall(r"\b\w+\b", text.lower())        
+     8                                                               return len(set(words))                              
+     9                                                                                                                   
+    10                                                                                                                   
+    11                                                           def count_unique_words2(file_path: str) -> int:         
+    12                                                               unique_words = []                                   
+    13                                                               with open(file_path, "r", encoding="utf-8") as file:
+    14                                                                   for line in file:                               
+    15     3%                                                                words = re.findall(r"\b\w+\b", line.lower())
+    16                                                                       for word in words:                          
+    17    73%                                                                    if word not in unique_words:            
+    18    16%                                                                        unique_words.append(word)           
+    19                                                               return len(unique_words)                            
+    20                                                                                                                   
+    21                                                                                                                   
+    22                                                           def count_unique_words3(file_path: str) -> int:         
+    23                                                               unique_words = set()                                
+    24                                                               with open(file_path, "r", encoding="utf-8") as file:
+    25                                                                   for line in file:                               
+    26     2%                                                                words = re.findall(r"\b\w+\b", line.lower())
+    27                                                                       for word in words:                          
+    28                                                                           unique_words.add(word)                  
+    29                                                               return len(unique_words)                            
+    30                                                                                                                   
+    31                                                                                                                   
+    32                                                           def main():                                             
+    33                                                               _result = count_unique_words1("book.txt")           
+    34                                                               _result = count_unique_words2("book.txt")           
+    35                                                               _result = count_unique_words3("book.txt")           
+    36                                                                                                                   
+    37                                                                                                                   
+    38                                                           if __name__ == "__main__":                              
+    39                                                               main()                                              
+    40                                                                                                                   
+                                                                 │
+╶──────┼───────┼───────┼───────┼────────┼───────┼───────────────┼───────┼─────────────────────────────────────────────────────────╴
+                                                                 function summary for example.py  
+     4     2%     2%         100%      30M ██████ 100%           count_unique_words1                                     
+    11    92%     1%                                             count_unique_words2                                     
+    22     3%                                                    count_unique_words3                                     
+       ╵       ╵       ╵       ╵        ╵       ╵               ╵       ╵
+Top AVERAGE memory consumption, by line:
+(1)     7:    30 MB
+Top PEAK memory consumption, by line:
+(1)     7:    30 MB
+(2)     6:    13 MB
+generated by the scalene profiler
+
+ +