Skip to content

Commit

Permalink
Add a real example
Browse files Browse the repository at this point in the history
  • Loading branch information
viluon committed Nov 25, 2023
1 parent 95cda02 commit 227b321
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions profiks-4.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
A = TypeVar("A")
B = TypeVar("B")

def profile(f: Callable[A, B], args: A):
def profile(f: Callable[A, B], *args: A):
# a log of f's execution
trajectory = []

sys.settrace(makeTrace(trajectory))
result = f(args)
result = f(*args)
sys.settrace(emptyTrace)
return [result, trajectory]

Expand Down Expand Up @@ -52,13 +52,51 @@ def uploadSummary(summary):
with requests.post("https://flamegraph.com", data=summary, headers=headers) as resp:
return resp.text

# functions to be profiled

def fib(x: int) -> int:
if x < 2:
return 1
return fib(x - 1) + fib(x - 2)

if __name__ == "__main__":
f = lambda x: uploadSummary(summariseTrajectory(profile(fib, x)[1]))
def wikiCommonWords():
import json
from urllib.request import urlopen

import collections
import operator
import sys

WIKIPEDIA_ARTICLE_API_URL = "https://en.wikipedia.org/w/api.php?action=query&titles=Spoon&prop=revisions&rvprop=content&format=json"

def download():
return urlopen(WIKIPEDIA_ARTICLE_API_URL).read()

def parse(json_data):
return json.loads(json_data)

def most_common_words(page):
word_occurences = collections.defaultdict(int)

for revision in page["revisions"]:
article = revision["*"]

for i in range(0, 7):
print(f(i))
for word in article.split():
if len(word) < 2:
continue
word_occurences[word] += 1

word_list = sorted(word_occurences.items(), key=operator.itemgetter(1), reverse=True)

return word_list[0:5]

data = parse(download())
page = list(data["query"]["pages"].values())[0]

sys.stderr.write("The most common words were %s\n" % most_common_words(page))



if __name__ == "__main__":
result = profile(lambda _: fib(7), None)
print(uploadSummary(summariseTrajectory(result[1])))

0 comments on commit 227b321

Please sign in to comment.