Title: Theory / Unimportant / CS / Programming / Python / Lib / Profiler 27.4;
Tags: computer_to_instruction FromInstruction hasNotNewEffectReturnsMeaning FromVariables fromCommandLine automatedNewLineAfterCommaSpace
On every note of this deck, unless a note says otherwise:
Page: https://docs.python.org/3.6/library/profile.html
Page: https://docs.python.org/3.6/library/profile.html
Page: https://docs.python.org/3.6/library/profile.html
Chapter: 27.4

Name: A profile
Definition: a set of statistics that describes how often and for how long various parts of the program executed.
Page: https://docs.python.org/3.6/library/profile.html
Chapter: 27
Section: 4

Question: Advantage of cProfile over profile
Answer: cProfile is quicker
Chapter: 27
Section: 4

Question: Advantage of profile over cProfile
Answer: profile is pure python, thus simpler to extend
Chapter: 27
Section: 4

Question: profiler module should NOT be used for
Answer: benchmarking
Chapter: 27
Section: 4

Variable: ncalls
Meaning: the number of calls (total/primitive)
Context: profile's output
Language: Python
Section: 2
Chapter: 27.4
Variables: \(f\) a function in python

Variable: tottime
Meaning: the total time spent in the given function (and excluding time made in calls to sub-functions)
Context: profile's output
Language: Python
Section: 2
Chapter: 27.4
Variables: \(f\) a function in python

Variable: percall
Meaning: quotient of the previous column (tottime/cumtime) divided by ncalls
Context: profile's output
Language: Python
Section: 2
Chapter: 27.4
Variables: \(f\) a function in python

Variable: cumtime
Meaning: the cumulative time spent in this and all subfunctions (from invocation till exit). This figure is accurate even for recursive functions.
Context: profile's output
Language: Python
Section: 2
Chapter: 27.4
Variables: \(f\) a function in python

Variable: filename:lineno(function)
Meaning: file in which function is defined, line number, and name
Context: profile's output
Language: Python
Section: 2
Chapter: 27.4
Variables: \(f\) a function in python

Name: A primitive call
Definition: A call of the function which is note a call by recursion
Page: https://docs.python.org/3.6/library/profile.html
Chapter: 27.4
Section: 2
Extra: base ?
Leaf?

Instruction: run("foo", bar, pop)
Effect: call exec("foo",bar, pop), and use stats on the value obtained
Language: Python
Section: 2
Library: cProfile or profile

Instruction: run("foo")
Effect: call exec("foo", __main__.__dict__, __main__.__dict__), and use stats on the value obtained
Language: Python
Section: 2
Where to use it: run, runctx
Library: cProfile or profile

Instruction: sort="bar"
Effect: choose to sort profile using function bar
Language: Python
Section: 2
Where to use it: run, runctx
Library: cProfile or profile

Instruction: filename="bar"
Effect: write the profile informations into file "bar"
Language: Python
Section: 2
Where to use it: run parameter
Library: cProfile or profile

Instruction: python -m cProfile foo.py
Effect: Run cProfile on the python script foo.py
Language: Python
Section: 2

Effect: Output cProfile's value in file bar
Language: python
Context: -m cProfile foo.py
Section: 2
Abbreviation: -o bar

Effect: Sort cProfile according to order bar
Language: python
Context: -m cProfile foo.py
Section: 2
Abbreviation: -s bar

Instruction: Profile()
Effect: create a class for most precise profiling. It can be used as profile/cProfile
Language: Python
Section: 2
Library: profile or cProfile

Instruction: Stats(p1,...,pn)
Effect: Create a Stats objects, containing the informations from all profiles or filenames pi
Language: Python
Section: 2
Library: pstats

Instruction: s.strip_dirs()
Effect: removes all leading path information from file names
Language: Python
Section: 2
Variables: s a Stats object

Instruction: s.add(p1,...,pn)
Effect: Add the elements pi and accumulates additional profiling information into the current profiling object.
Language: Python
Section: 2
Variables: s a Stats object

Instruction: s.dump_stats(foo)
Effect: Save the data loaded into the Stats object to a file named foo
Language: Python
Section: 2
Variables: s a Stats object

Instruction: s.sort_stats(k1,...,kn)
Effect: Sort the line of s according to the keys ki
Language: Python
Section: 2
Variables: s a Stats object

Instruction: s.reverse_order()
Effect: Reverse the ordering of the lines of stats
Language: Python
Section: 2
Variables: s a Stats object

Instruction: s.print_stats(foo)
Effect: prints out a report as described in the profile.run()
Language: Python
Section: 2
Variables: s a Stats object

Instruction: n a number
Effect: restrict printing to the n first lines
Language: Python
Context: stats
Section: 2
Variables: s a Stats object
Where to use it: s.print_stats()

Instruction: r a float between 0 and 1
Effect: restrict printing to r percent of the lines
Context: Python
Section: 2
Variables: s a Stats object
Where to use it: s.print_stats()

Instruction: str a string
Effect: restrict printing to lines matching the regexp str
Language: Python
Section: 2
Variables: s a Stats object
Where to use it: s.print_stats()

Instruction: print_callers(r1,...,rn)
Effect: prints a list of all functions that called each function in the profiled database. The ordering is identical to that provided by print_stats(), and the definition of the restricting argument is also identical. Each caller is reported on its own line.
Language: Python
Section: 2
Variables: s a Stats object

Instruction: print_callees(r1,...,rn)
Effect: prints a list of all function that were called by the indicated function. Aside from this reversal of direction of calls (re: called vs was called by), the arguments and ordering are identical to the print_callers() method.
Language: Python
Section: 2
Variables: s a Stats object

Name: Deterministic Profiling
Definition: Profiling meant to reflect the fact that all function call, function return, and exception events are monitored, and precise timings are made for the intervals between these events
Page: https://docs.python.org/3.6/library/profile.html
Chapter: 27.4
Section: 5

Name: Statistical profiling
Definition: Profiling meant to randomly samples the effective instruction pointer, and deduces where time is being spent.
Page: https://docs.python.org/3.6/library/profile.html
Chapter: 27.4
Section: 5

Question: Advantage of statistical profiling
Answer: less overhead (as the code does not need to be instrumented)
Chapter: 27.4
Section: 5

Question: Advantage of deterministic profiling
Answer: Provide more indications of where time is being spent
Chapter: 27.4
Section: 5

Question: Why does Python profiler prefer deterministic over statistical
Answer: Python is interpreted, there is already much overhead, and already hook for each event, which allow to call the profiler
Chapter: 27.4
Section: 5

Question: How to identify inline-expansion points
Answer: high call counts in call count statistics
Chapter: 27.4
Section: 5
Context: Python profiler

Question: How to identify bugs in code using call count statistics
Answer: search surprising counts
Chapter: 27.4
Section: 5

Question: Main limitation of profiling in python
Answer: clock accuracy can change the profile's result
Chapter: 27.4
Section: 6
Answer2: profiler take time, which may be considered in the clock measurement

Question: what means negative number in time
Answer: The profiler is calibrated and results are actually better than without calibration.
Chapter: 27.4
Section: 6
Context: Python profiler

Instruction: pr.calibrate(foo)
Effect: executes the number of Python calls given by the argument, directly and again under the profiler, measuring the time for both. It then computes the hidden overhead per profiler event
Language: Python
Section: 7
Returns: The time (which one?) as a float.
Variables: pr a profiler object

Instruction: profile.Profile(foo)
Effect: Create a profiler with time function foo
Language: Python
Section: 7
Assuming: foo should return a single number, or a list of numbers whose sum is the current time

Instruction: cProfile.Profile(foo)
Effect: Create a profiler with time function foo
Language: Python
Section: 7
Assuming: foo should return a single number