Questions tagged [line-profiler]

Use this tag for questions about the Python "line_profiler" package. If you have a question about performance, or how to optimize your code based on "line_profiler" results, please use the more generic "performance" or "optimization" tags instead.

line_profiler is an external package allowing to measure the execution time (both relative and absolute) spent in each line. This can be an important utility to find bottlenecks or hotspots in python functions.

66 questions
4
votes
0 answers

How to use line_profiler to profile all modules in a package?

line_profiler is a great Python package to find performance bottlenecks. The only complication when using it is that we have to specify each profiled module one by one on the command line: %lprun -m toppkg.pkg1.module11 -m toppkg.pkg1.module12 ...…
Mark Horvath
  • 1,136
  • 1
  • 9
  • 24
3
votes
0 answers

Why is Line profiler showing only partial results?

My line profiler %lprun -f works but does not show the line contents but only the numbers. So it is useless for me. It shows some directory problem as below. Timer unit: 1e-07 s Total time: 0.962042 s Could not find file C:\Users\my…
3
votes
1 answer

Kernprof (line_profiler): how to output result as text instead of a binary file

When running kernprof: kernprof -l script_to_profile.py The output is stored in a binary file, which can be read in the terminal/command line. Is there a way to output the results to a text file? This seems like useful functionality to have, but…
PvK
  • 340
  • 2
  • 15
3
votes
0 answers

python line_profiler (kernprof) not working with minimal example

Here's my script.py: @profile def primes(n): if n == 2: return [2] elif n < 2: return [] s = range(3, n + 1, 2) mroot = n ** 0.5 half = (n + 1) / 2 - 1 i = 0 m = 3 while m <= mroot: if…
lollercoaster
  • 15,969
  • 35
  • 115
  • 173
3
votes
2 answers

Using the line-profiler (in ipython) on compiled Cython code

I read the answer to this question How to profile cython functions line-by-line, but I can't seem to get it to work with my setup. I have a cumsum.pyx file: # cython: profile=True # cython: linetrace=True # cython: binding=True DEF CYTHON_TRACE =…
user357269
  • 1,835
  • 14
  • 40
3
votes
0 answers

Using line_profiler to profile Pyramid views

I have a trouble profiling a Pyramid view with line_profiler. Since Pyramid's @view_config can't be used together with @profile in typical fashion, I modified the view decorator I use for other purpose: def view_decorator_profile(view_callable): …
LetMeSOThat4U
  • 6,470
  • 10
  • 53
  • 93
3
votes
2 answers

How can I profile only my code in Python or IPython?

I'm familiar with the %prun magic command in IPython that uses the Python profile module. However, I'd like to only profile my code. That is, I'd like to see which are the slowest lines in my Python code, not the lines buried deep in some external…
user1507844
  • 5,973
  • 10
  • 38
  • 55
2
votes
1 answer

How can I do %lprun work on nested function in python?

In jupyter Notebook, I am trying to use %lprun on nested functions but I do not sucess. The following code, in a notebook cell def outerfoo(): def innerfoo(): print("Hello World") print("Good Bye") innerfoo() %lprun -f…
Stef1611
  • 1,978
  • 2
  • 11
  • 30
2
votes
0 answers

How to use pytest, hypothesis and line_profiler / kernprof together?

To figure out Why is this so slow? I wanted to profile my example generation using line_profiler. However, I can't get it to work. Here's what I tried: kernprof -m pytest -n 3 tests/test_conv.py --hypothesis-profile default pytest -m line_profiler…
phdoerfler
  • 470
  • 6
  • 19
2
votes
0 answers

No module named 'line_profiler._line_profiler' when calling line_profiler

I intended to test my code with line_profiler in the following way in Pycharm but got an error just like the title said. I tried in both miniconda 3.8 and python 3.7 (Windows) but got the same error and I don't know how to fix that. I visited the…
Mingzhe
  • 31
  • 5
2
votes
1 answer

Why line_profiler enable and disable output only 'Timer unit'?

enviroment python 3.9 line-profiler 3.1.0 I installed line-profiler by pip. sudo pip3 install line-profiler detail add_function and runcall works. that's ok. import line_profiler def hoge(): n = 0 for i in range(100): n += i …
miwarin
  • 123
  • 5
2
votes
1 answer

Line profiling class instantiation in python

I have some existing code that I'm trying to profile. I can successfully line profile class methods by adding a @profile decorator using kernprof. Is there a general way to profile class instantiation? I have a few classes that have a quite complex…
naught101
  • 18,687
  • 19
  • 90
  • 138
2
votes
2 answers

Find the number of times a combination occurs in a numpy 2D array

I have a 2D numpy array, and I want a function operating on col1 and col2 of the array, If 'M' is the number of unique values from col1 and 'N' is the number of unique values from col2, then the output 1D array will have size (M * N) For example,…
Bidisha Das
  • 177
  • 2
  • 11
2
votes
1 answer

Optimized method to partition numpy 2D array

I am trying to partition a 2D numpy array into 2 separate numpy arrays based on the contents of a particular column. This is my code: import numpy as np import pandas as pd @profile def partition_data(arr,target_colm): total_colms =…
Bidisha Das
  • 177
  • 2
  • 11
2
votes
2 answers

How can I time-profile unittests in Python?

I have a test suite and code it is testing. If I put from memory_profiler import profile at the tops of the appropriate files, decorate the functions I want profiled with @profile, and run in the standard way with python TestThing.py, I get great…
Pavel Komarov
  • 1,153
  • 12
  • 26