Quick Start¶
This notebook demonstrates two display settings: a compact summary after each cell and one full report at the end. Both use the default Trace backend with driver memory collection.
Cell summaries¶
Start with display="cell-summary" to show each cell's source and measurements
immediately below it. The final cell displays the cumulative report.
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") |
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 | 66.5 µs | 1 | 0 B | 0 B | from collections import Counter |
| 2 | 26.5 µs | 1 | 0 B | 0 B | import re |
| 5 | 29.4 µs | 1 | 0 B | +379 B | def tokenize(document: str) -> list[str]: |
| 19 | 21.78 ms | 300 | 0 B | +314.7 KB | return re.findall(r"[a-z]+", document.lower()) |
| 22 | 398.4 µs | 2 | 0 B | 0 B | documents = [ |
| 23 | 54.30 ms | 300 | +4.1 KB | +54.6 KB | f"Analysis {number}: notebooks keep Python source beside its results. " |
| 24 | — | — | — | — | "A profiler helps find repeated parsing, sorting, and aggregation work." |
| 25 | 53.23 ms | 302 | +69.6 KB | 0 B | for number in range(300) |
| 26 | — | — | — | — | ] |
| 27 | 113.38 ms | 301 | 0 B | +2.5 KB | tokens = [tokenize(document) for document in documents] |
| 28 | 1.19 s | 5,702 | +4.1 KB | +3.8 KB | frequencies = Counter(word for words in tokens for word in words) |
| 29 | 1.25 ms | 1 | 0 B | +1.9 KB | common = frequencies.most_common(10) |
| 30 | 38.2 µs | 1 | 0 B | +144 B | unique_words = sorted(frequencies) |
| 31 | 30.2 µs | 1 | 0 B | +28 B | total = sum(frequencies.values()) |
| 32 | 1.28 ms | 1 | 0 B | +4.2 KB | print(f"Tokens: {total}; vocabulary: {len(unique_words)}") |
| 33 | 371.9 µs | 1 | 0 B | +1.2 KB | print(common) |
One report for several cells¶
Continue the same session with the next two cells. Each compact summary shows only that cell's measurements. The full overview at the end includes all cells.
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.60 ms | 1 | 0 B | +111.8 KB | values = list(range(3000)) |
| 2 | 26.2 µs | 1 | 0 B | +64 B | buckets = {} |
| 3 | 591.80 ms | 3,001 | +233.5 KB | 0 B | for value in values: |
| 4 | 700.91 ms | 3,000 | +200.7 KB | +124.4 KB | buckets.setdefault(value % 20, []).append(value * value) |
| 5 | 667.46 ms | 3,021 | +155.6 KB | +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 | — | — | — | — | documents = [ |
| 23 | — | — | — | — | f"Analysis {number}: notebooks keep Python source beside its results. " |
| 24 | — | — | — | — | "A profiler helps find repeated parsing, sorting, and aggregation work." |
| 25 | — | — | — | — | for number in range(300) |
| 26 | — | — | — | — | ] |
| 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) |
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 | 122.3 µs | 1 | 0 B | 0 B | average = sum(squares) / len(squares) |
| 2 | 3.88 ms | 21 | 0 B | +2.3 KB | summaries = {key: sum(group) / len(group) for key, group in buckets.items()} |
| 3 | 4.14 ms | 21 | 0 B | +96 B | largest = sorted(summaries.items(), key=lambda item: item[1], reverse=True)[:5] |
| 4 | 548.9 µs | 1 | 0 B | +2.2 KB | print(f"Average square: {average:,.1f}; largest groups: {largest}") |
Finish and inspect the full overview¶
Stop collection and retrieve the normalized result through profile.result
(also available as session.result). profile.stop() returns None so it adds
no text output. Save the report and display it inline once, in the final cell.
show() returns None so the cell displays only the report. Use session.html()
to retrieve the HTML string without displaying it.
Captured source stays in the report even if you later edit these cells. Review source snapshots before sharing.
profile.stop()
result = profile.result
report_path = session.save("notebook.html")
session.show(inline=True)
One report at the end¶
Use display="end" to keep ordinary cell outputs visible while collecting
measurements across cells. LineScope displays one complete report when you
call profile.stop().
from linescope import profile
end_session = profile.start(memory=True, display="end")
values = list(range(3000))
squared = [value * value for value in values]
print(f"Prepared {len(squared):,} squares")
Prepared 3,000 squares
mean_square = sum(squared) / len(squared)
print(f"Mean square: {mean_square:,.1f}")
Mean square: 2,998,500.2
profile.stop()
end_result = profile.result
end_report_path = end_session.save("notebook-end.html")