Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change the Timer unit to seconds instead of 1e-9 seconds #291

Closed
ogencoglu opened this issue Oct 5, 2024 · 2 comments
Closed

How to change the Timer unit to seconds instead of 1e-9 seconds #291

ogencoglu opened this issue Oct 5, 2024 · 2 comments

Comments

@ogencoglu
Copy link

The Time column is hard to understand when Timer unit: 1e-09 s:

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    42      1872    1621000.0    865.9      0.1    
    43       936     136000.0    145.3      0.0          
    44       936   65239000.0  69699.8      2.4            
    45       936   46525000.0  49706.2      1.7                             
    47       936     163000.0    174.1      0.0          
    49      1872    9938000.0   5308.8      0.4      
    50      1872    8823000.0   4713.1      0.3       
    51       936    6674000.0   7130.3      0.2         
    52       936      95000.0    101.5      0.0    
    54      1872  721673000.0 385509.1     26.8     
    55       936    3798000.0   4057.7      0.1                             

Is there a way to show seconds rounded to 1 or 2 decimals? 7.22 is more readable than 721673000.0. How to control this?

@Erotemic
Copy link
Member

Erotemic commented Oct 5, 2024

See the unit argument:

python -m line_profiler --help
usage: __main__.py [-h] [-V] [-u UNIT] [-z] [-r] [-t] [-m] profile_output

positional arguments:
  profile_output        *.lprof file created by kernprof

options:
  -h, --help            show this help message and exit
  -V, --version         show program's version number and exit
  -u UNIT, --unit UNIT  Output unit (in seconds) in which the timing info is displayed (default: 1e-6)
  -z, --skip-zero       Hide functions which have not been called
  -r, --rich            Use rich formatting
  -t, --sort            Sort by ascending total time
  -m, --summarize       Print a summary of total function time

Example:

   echo "if 1:
        from line_profiler import profile

        @profile
        def is_prime(n):
            max_val = n ** 0.5
            stop = int(max_val + 1)
            for i in range(2, stop):
                if n % i == 0:
                    return False
            return True


        @profile
        def find_primes(size):
            primes = []
            for n in range(size):
                flag = is_prime(n)
                if flag:
                    primes.append(n)
            return primes


        @profile
        def main():
            print('start calculating')
            primes = find_primes(10)
            primes = find_primes(1000)
            primes = find_primes(100000)
            print(f'done calculating. Found {len(primes)} primes.')


        if __name__ == '__main__':
            main()
   " > script.py


   LINE_PROFILE=1 python script.py

   # Use different values for the unit report
   python -m line_profiler -rtmz --unit 1 profile_output.lprof
   python -m line_profiler -rtmz --unit 1e-6 profile_output.lprof
   python -m line_profiler -rtmz --unit 1e-9 profile_output.lprof

I made a documentation update to make this more clear: #293

@ogencoglu
Copy link
Author

Thanks for the swift reply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants