Quick Start¶
Profile notebook cells with Trace and memory collection. Compare compact cell summaries with one report at the end.
Cell summaries¶
Use display="cell-summary" for measurements below each cell.
Copied!
from linescope import profile
session = profile.start(memory=True, display="cell-summary")
from linescope import profile
session = profile.start(memory=True, display="cell-summary")
| 1 | — | — | — | — | from linescope import profile |
| 3 | — | — | — | — | session = profile.start(memory=True, display="cell-summary") |
Copied!
from collections import Counter
import re
def tokenize(document: str) -> list[str]:
"""Extract lowercase words from one document.
Parameters
----------
document : str
Text to split into alphabetic words.
Returns
-------
list[str]
Lowercase words in their original order.
"""
return re.findall(r"[a-z]+", document.lower())
documents = [
f"Analysis {number}: notebooks keep Python source beside its results. "
"A profiler helps find repeated parsing, sorting, and aggregation work."
for number in range(300)
]
tokens = [tokenize(document) for document in documents]
frequencies = Counter(word for words in tokens for word in words)
common = frequencies.most_common(10)
unique_words = sorted(frequencies)
total = sum(frequencies.values())
print(f"Tokens: {total}; vocabulary: {len(unique_words)}")
print(common)
from collections import Counter
import re
def tokenize(document: str) -> list[str]:
"""Extract lowercase words from one document.
Parameters
----------
document : str
Text to split into alphabetic words.
Returns
-------
list[str]
Lowercase words in their original order.
"""
return re.findall(r"[a-z]+", document.lower())
documents = [
f"Analysis {number}: notebooks keep Python source beside its results. "
"A profiler helps find repeated parsing, sorting, and aggregation work."
for number in range(300)
]
tokens = [tokenize(document) for document in documents]
frequencies = Counter(word for words in tokens for word in words)
common = frequencies.most_common(10)
unique_words = sorted(frequencies)
total = sum(frequencies.values())
print(f"Tokens: {total}; vocabulary: {len(unique_words)}")
print(common)
Tokens: 5400; vocabulary: 18
[('analysis', 300), ('notebooks', 300), ('keep', 300), ('python', 300), ('source', 300), ('beside', 300), ('its', 300), ('results', 300), ('a', 300), ('profiler', 300)]
| 1 | 44.6 µs | 1 | +114.7 KB | 0 B | from collections import Counter |
| 2 | 20.2 µs | 1 | +28.7 KB | 0 B | import re |
| 5 | 24.0 µs | 1 | +49.2 KB | +379 B | def tokenize(document: str) -> list[str]: |
| 19 | 16.82 ms | 300 | +479.2 KB | +314.7 KB | return re.findall(r"[a-z]+", document.lower()) |
| 22-26 | 308.4 µs | 2 | +8.2 KB | 0 B | documents = [
f"Analysis {number}: notebooks keep Python source beside its results. "
"A profiler helps find repeated parsing, sorting, and aggregation work."
for number in range(300)
] |
| 22-26 | 45.35 ms | 300 | +106.5 KB | +54.6 KB | documents = [
f"Analysis {number}: notebooks keep Python source beside its results. "
"A profiler helps find repeated parsing, sorting, and aggregation work."
for number in range(300)
] |
| 22-26 | 44.95 ms | 302 | +184.3 KB | 0 B | documents = [
f"Analysis {number}: notebooks keep Python source beside its results. "
"A profiler helps find repeated parsing, sorting, and aggregation work."
for number in range(300)
] |
| 27 | 91.54 ms | 301 | +704.5 KB | +2.5 KB | tokens = [tokenize(document) for document in documents] |
| 28 | 1.02 s | 5,702 | +938.0 KB | +3.8 KB | frequencies = Counter(word for words in tokens for word in words) |
| 29 | 1.02 ms | 1 | +4.1 KB | +1.9 KB | common = frequencies.most_common(10) |
| 30 | 30.4 µs | 1 | 0 B | +144 B | unique_words = sorted(frequencies) |
| 31 | 34.9 µs | 1 | +16.4 KB | +28 B | total = sum(frequencies.values()) |
| 32 | 1.38 ms | 1 | +57.3 KB | +4.1 KB | print(f"Tokens: {total}; vocabulary: {len(unique_words)}") |
| 33 | 297.1 µs | 1 | +45.1 KB | +1.2 KB | print(common) |
One report for several cells¶
Continue the session. The final report includes all profiled cells.
Copied!
values = list(range(3000))
buckets = {}
for value in values:
buckets.setdefault(value % 20, []).append(value * value)
squares = [square for group in buckets.values() for square in group]
values = list(range(3000))
buckets = {}
for value in values:
buckets.setdefault(value % 20, []).append(value * value)
squares = [square for group in buckets.values() for square in group]
| 1 | 2.39 ms | 1 | 0 B | +111.8 KB | values = list(range(3000)) |
| 2 | 21.3 µs | 1 | 0 B | +64 B | buckets = {} |
| 3 | 509.61 ms | 3,001 | +28.7 KB | 0 B | for value in values: |
| 4 | 508.18 ms | 3,000 | +28.7 KB | +124.4 KB | buckets.setdefault(value % 20, []).append(value * value) |
| 5 | 516.60 ms | 3,021 | 0 B | +26.0 KB | squares = [square for group in buckets.values() for square in group] |
| 1 | — | — | — | — | from collections import Counter |
| 2 | — | — | — | — | import re |
| 5 | — | — | — | — | def tokenize(document: str) -> list[str]: |
| 19 | — | — | — | — | return re.findall(r"[a-z]+", document.lower()) |
| 22-26 | — | — | — | — | documents = [
f"Analysis {number}: notebooks keep Python source beside its results. "
"A profiler helps find repeated parsing, sorting, and aggregation work."
for number in range(300)
] |
| 27 | — | — | — | — | tokens = [tokenize(document) for document in documents] |
| 28 | 0 µs | 0 | 0 B | -55 B | frequencies = Counter(word for words in tokens for word in words) |
| 29 | — | — | — | — | common = frequencies.most_common(10) |
| 30 | — | — | — | — | unique_words = sorted(frequencies) |
| 31 | — | — | — | — | total = sum(frequencies.values()) |
| 32 | — | — | — | — | print(f"Tokens: {total}; vocabulary: {len(unique_words)}") |
| 33 | — | — | — | — | print(common) |
Copied!
average = sum(squares) / len(squares)
summaries = {key: sum(group) / len(group) for key, group in buckets.items()}
largest = sorted(summaries.items(), key=lambda item: item[1], reverse=True)[:5]
print(f"Average square: {average:,.1f}; largest groups: {largest}")
average = sum(squares) / len(squares)
summaries = {key: sum(group) / len(group) for key, group in buckets.items()}
largest = sorted(summaries.items(), key=lambda item: item[1], reverse=True)[:5]
print(f"Average square: {average:,.1f}; largest groups: {largest}")
Average square: 2,998,500.2; largest groups: [(19, 3027047.6666666665), (18, 3024030.6666666665), (17, 3021015.6666666665), (16, 3018002.6666666665), (15, 3014991.6666666665)]
| 1 | 120.7 µs | 1 | 0 B | 0 B | average = sum(squares) / len(squares) |
| 2 | 3.01 ms | 21 | 0 B | +2.5 KB | summaries = {key: sum(group) / len(group) for key, group in buckets.items()} |
| 3 | 3.05 ms | 21 | 0 B | +96 B | largest = sorted(summaries.items(), key=lambda item: item[1], reverse=True)[:5] |
| 4 | 578.5 µs | 1 | 0 B | +2.2 KB | print(f"Average square: {average:,.1f}; largest groups: {largest}") |
Finish and inspect the full overview¶
Stop collection, save the report, and display it inline. Results are available
through session.result or profile.result.
Copied!
profile.stop()
result = profile.result
report_path = session.save("notebook.html")
session.show(inline=True)
profile.stop()
result = profile.result
report_path = session.save("notebook.html")
session.show(inline=True)
One report at the end¶
Use display="end" to show one report when profile.stop() runs.
Copied!
from linescope import profile
end_session = profile.start(memory=True, display="end")
from linescope import profile
end_session = profile.start(memory=True, display="end")
Copied!
values = list(range(3000))
squared = [value * value for value in values]
print(f"Prepared {len(squared):,} squares")
values = list(range(3000))
squared = [value * value for value in values]
print(f"Prepared {len(squared):,} squares")
Prepared 3,000 squares
Copied!
mean_square = sum(squared) / len(squared)
print(f"Mean square: {mean_square:,.1f}")
mean_square = sum(squared) / len(squared)
print(f"Mean square: {mean_square:,.1f}")
Mean square: 2,998,500.2
Copied!
profile.stop()
end_result = profile.result
end_report_path = end_session.save("notebook-end.html")
profile.stop()
end_result = profile.result
end_report_path = end_session.save("notebook-end.html")